最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - In a WordPress plugin, how do you output HTML code inside the DOM header?

programmeradmin1浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

HTML:

<header>
   <!-- I want to add html code here -->
</header>

Function:

 function add_html_to_header {
   <b> Hello World </b>
 }
 add_action('','add_html_to_header')

Expected Html results:

<header id="header">
    <b> Hello World </b>
</header>

EDIT: Is it possible without using Javascript?

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

HTML:

<header>
   <!-- I want to add html code here -->
</header>

Function:

 function add_html_to_header {
   <b> Hello World </b>
 }
 add_action('','add_html_to_header')

Expected Html results:

<header id="header">
    <b> Hello World </b>
</header>

EDIT: Is it possible without using Javascript?

Share Improve this question edited Aug 2, 2019 at 9:14 John Dee 5135 silver badges14 bronze badges asked Aug 31, 2017 at 1:56 Van Adrian CabreraVan Adrian Cabrera 1391 silver badge5 bronze badges 9
  • First of all, enable PHP's error_reporting(E_ALL); and WP_DEBUG. You'll see your syntax is seriously bad. – Max Yudin Commented Sep 3, 2017 at 10:32
  • Why do you need to add it using a function, rather than putting the html straight into the page (or header etc) template? – Monkey Puzzle Commented Sep 4, 2017 at 6:09
  • Related to stackoverflow/questions/772510/… – John Dee Commented Aug 2, 2019 at 4:13
  • Related to youtube/watch?v=6UNSafWC9a8 – John Dee Commented Aug 2, 2019 at 4:23
  • It's quite difficult! Obviously the problem lies that you're trying to insert - -- right in the middle --- of the header.php template file. Could we filter that file somehow? Mayber, conditionally override the file, and string replace the </head> tag somehow? – John Dee Commented Aug 2, 2019 at 4:27
 |  Show 4 more comments

2 Answers 2

Reset to default 4

You can add html to the site head using this function:

// Add scripts to wp_head()
function add_head_html() { ?>
    <!-- html goes here -->
    <?php }
    add_action( 'wp_head', 'add_head_html' );

But if you're talking about the html element <header> (as it sounds like from your edits) it sounds like overcomplicating things, but you might want to look at injecting it with jquery into the right spot using a solution such as this: https://stackoverflow/a/9866637/3387817

You can use action hooks:

<header>
   <!-- I want to add html code here -->
   <?php do_action('wpse_myheader'); ?>
</header>

Then you can use:

function add_html_to_header { ?>
   <b> Hello World </b>
<?php }
 add_action('wpse_myheader','add_html_to_header');
发布评论

评论列表(0)

  1. 暂无评论