0

I would like to write a plugin which injects specific content to every non admin page. This is my first plugin.

I've figured out how can I inject content to every non admin page's head:

<?php
    function myfunction() {
?>
<!-- My injected content.... -->
....
<?php
}

if (!is_admin()) {
  add_action('wp_head', 'myfunction');
}

Unfortunately I can not figure out how to inject to the body, say an element, because I did not find the appropriate action.

  • Injecting a script to head, which injects elements to DOM is not a solution this case, because the injected content must be there at load time, so it may be late to modify the DOM after load.
  • The injected content must be "calculated" and the only place it can be calculated is php serverside.
4
  • 2
    You may want to take a look at the WordPress the_content filter developer.wordpress.org/reference/functions/the_content Commented Mar 22, 2016 at 23:00
  • does the injected html depend of the body content or it can be calculated before the generation of the body ?
    – mmm
    Commented Mar 22, 2016 at 23:05
  • @mmm: It does not depend on the body content Commented Mar 22, 2016 at 23:35
  • why not use wp_footer instead of wp_head, that is inside the body at the bottom of the page. if it is just for a script and you don't care where the content outputs that would be ideal.
    – majick
    Commented Mar 23, 2016 at 6:44

1 Answer 1

-2

You can use the is_page() function or is_single() function to echo content to front end pages

function add_content_to_page {

if ( is_page() ) {
echo '<p>content</p>';
 }   
}

or combine the two :

function add_content_to_page {

if ( is_page() || is_single() ) {
echo '<p>content</p>';
 }   
}

Similarly you can do is_page(41) to target a specific page id or is_home() || is_front_page() to target the homepage alone there a lot more options and examples in the WordPress codex

1
  • 1
    This is no way answers the question asked.
    – vancoder
    Commented Mar 22, 2016 at 23:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.