Why does wp_head hook my functions to the beginning of my source code?
It doesn’t. You’ve written the PHP tag incorrectly, so your code is being printed to the browser instead of being run. You have <php?. It’s supposed to be <?php.
It doesn’t. You’ve written the PHP tag incorrectly, so your code is being printed to the browser instead of being run. You have <php?. It’s supposed to be <?php.
You’ve misunderstood how hooks, and PHP, work. Hooks are not persistent. Each time you visit a page in WordPress the PHP scripts that comprise WordPress all get run. Every time. This includes plugins and the theme’s the functions.php file. So when you run add_action() that queues up a callback function to run whenever the corresponding … Read more
I think it’s really a question of whether that code is giving the correct hook name for the given page. Here’s a simple function you can use to output the hook suffix on each admin page, it will appear inside a red error box(so it’ll be easy to spot) for admins only. add_action( ‘admin_notices’, ‘print_admin_pagehook’ … Read more
Not sure what you mean by ‘display all categories’, i thought it does that by default? Anyways…you can hook into it, using the following filter hooks: widget_categories_args widget_categories_dropdown_args Both hooks pass the query args to get the categories as an array. The default is array(‘orderby’ => ‘name’, ‘show_count’ => $c, ‘hierarchical’ => $h), wherein $c … Read more
Completely skipping wp_head and wp_footer doesn’t seem like a good idea, many core components and plugins depend on these hooks. Instead, you could create a light header and footer, and load them via an extra argument to get_header() and get_footer(). get_header( ‘light’ ) will load header-light.php instead of the regular header file. Create one without … Read more
Enqueue mechanics and suggested ways to use them serve flexibility and interoperability. Simply put consider what would be the easiest way for third party code to modify mechanics of yours? Hardcoding things in template often makes editing that template the only viable option. Manipulating hooks is typically more convenient and less problematic than that. On … Read more
It seems you’re actually looking for the the_content filter. add_filter(‘the_content’, ‘modify_content’); function modify_content($content) { global $post; if ($post->ID != $desired_id) return $content; $modified_content = /* modify content here */; return $modified_content; }
Don’t remove wp_head() or wp_footer() as they are required for triggering other hooks. Instead, conditionally remove scripts / stylesheets based on page type, page ID, page template, post category, etc. I reckon you mostly need to worry about this on the site’s front page, since all your site CSS and main JavaScript will be loaded … Read more
Use get_post_format(): function test_filter($content) { $format = get_post_format(); if ( ! $format ) return $content; if ( ‘audio’ === $format ) // do something with audio if ( ‘aside’ === $format ) // do something with aside return “$content <hr>post format: $format”; } Since the_content() requires a global $post object to work, get_post_format() will always … Read more
Filters and Actions are similar under the hood, but with an important difference- Actions allow you to do something when an event occurs- WordPress does something and executes an action to let you do your own thing. A function hooked to an action may or may not produce output, but it never returns a value … Read more