Do you have <?php wp_head(); ?>
in your header.php file?
I was having the same problem with another plug-in and discovered that the hook was missing. Your add_action won’t work if it’s is missing.
i put add_action(‘wp_head’, ‘add_css’); in “my_function_one” so when the plugin works, it also add “add_css” in wp_head.
in the frontpage and page my “add_css” function is not executed
Two things to check:
a) If you’re doing the add_action from inside a function, then that function must actually be called before the action occurs. Really, it’s generally better to have the add_action in your plugin but outside any function code, except in certain cases.
b) On these pages where the plugin is not being executed, check the templates to ensure that wp_head() is being called. The wp_head() call should *always* be in the header of every page on the site, but some themes don’t have it there in all cases.
Thread Starter
hardi
(@hardi)
Of course i have <?php wp_head(); ?> in my header.php.
Update:
add_action(‘wp_head’, ‘add_css’); works well in page and single post but not works in frontpage and archive page
@otto:
a. Yups the function is called cause that function is find and replace in the_content section.
Example in my plugin:
function insert() {
preg_match(bla..bla..bla..)
and
replace(bla..bla..bla..)
add_filter(‘wp_head’, ‘add_css’);
}
add_filter(‘the_content’, ‘insert’);
I want this cause i don’t want to make the page size bigger with that css when the html doesn’t need it. Big page size it’s mean slow page loading.
@otto:
a. Yups the function is called cause that function is find and replace in the_content section.
Example in my plugin:
function insert() {
preg_match(bla..bla..bla..)
and
replace(bla..bla..bla..)
add_filter(‘wp_head’, ‘add_css’);
}
add_filter(‘the_content’, ‘insert’);
No, that’s no good. See, the_content is only called when you’re actually displaying a post. By then, the wp_head function has already ran, and so your CSS is not going to be inserted. Basically, you’re doing this:
Add filter to the_content.
Run header and wp_head.
Run the_content.
Add filter to wp_head.
The filter you’re adding to wp_head never runs because wp_head is not called again later.
Change it to this:
function insert() {
preg_match(bla..bla..bla..)
and
replace(bla..bla..bla..)
}
add_filter('the_content', 'insert');
add_filter('wp_head', 'add_css');
If you want to save bandwidth, have add_css add a link/import to a separate stylesheet file instead of actually putting the CSS code in the document itself. This way, the stylesheet file is cached at the browser and it won’t pull it again unless it needs to do so.
Thread Starter
hardi
(@hardi)
Uhmm.. Maybe the standard add_action or add_filter outside function like you said is the best for now.
Or maybe someone can do this?