How can I use add_filter for a specific page template? - wordpress

In Wordpress I have a page template called designers.php.
When loading, it reads the slug to get a uniqe ID, then calls the DB to retrieve designer information.
I want to use this information to alter the page title, using the designer name in the title tag.
I've tried using the add_filter in my designers.php file, but it's not working:
add_filter('wp_title', 'set_page_title');
function set_page_title($title) {
global $brand;
return 'Designer '.$brand['name'].' - '.get_bloginfo('name');
}
I'm, guessing the add_filter must either be located inside a plugin or in functions.php file.
How can I achieve what I'm trying to do?
UPDATE
The function is never fired as long as I use wp_title. If I change it to init (for testing), the function is fired.
So why does the add_filternor work for wp_title?

You are almost right. The filter must reside in function.php, and is called to modify the title. You can add the filter conditionally. Use this function is_page_template() to determine if wordpress is rendering your template
Try to modify your function like this:
add_filter('wp_title', 'set_page_title');
function set_page_title($title) {
global $brand;
if (is_page_template('designer.php'))
return 'Designer '.$brand['name'].' - '.get_bloginfo('name');
else
return $title;
}

First of all add_filter must either be located inside a plugin or in functions.php file.
Then, maybe you have to set the priority for the filter :
add_filter('wp_title', 'set_page_title',1);
function set_page_title() {
global $brand;
return 'Designer '.$brand['name'].' - '.get_bloginfo('name');
}
And check the <title> meta in your header.php theme.
<title><?php wp_title(); ?></title>

Related

Get current page id and page title inside the theme functions.php

Is it possible to get the current page id and page title inside the current themes functions.php?.
Note: I'm not referring post id.
I have tried following functions inside the themes->twentytwentytwo->functions.php file and did not work.
prin_r(the_title());
print_r(get_queried_object_id());
print_r(get_the_title());
From what I understand functions.php is included too early to use $post or get_queried_object_id().
What I've done before is create a function in functions.php that you can call in your template and pass it the id or the title.
For instance in your functions.php:
function myPage($pageId, $pageTitle) {
//Do whatever you want and return the result
}
Then in your template just call the function:
<?php
$myPage(get_the_ID(), get_the_title());
?>
Hope it helps

WP load scripts if template has shortcode

I have a plugin, it has many css theme files. Of course I do not want to load all of them, only one. It depends on config. For post I use has_shortcode function, but how todo the same thing with template that use do_shortcode function.
Note:
I found a good solution, I use
$this->loader->add_action( 'init', $plugin_public, 'register_scripts');
$this->loader->add_action( 'wp_footer', $plugin_public, 'print_scripts');
Inside shortcode handle I set a global var to true
global $imagelink_plugin_shortcode_used;
$imagelink_plugin_shortcode_used = true;
The function print_scripts add my scripts if my global var is true
public function print_scripts() {
global $imagelink_plugin_shortcode_used;
if ( ! $imagelink_plugin_shortcode_used )
return;
wp_print_scripts($this->plugin_name . '-imagelinks');
}
Thanks for answers.
Instead of using has_shortcode function, what you can do is register and enqueue those files when that shortcode is rendered.
First, register your css files with wp_register_script. Make sure to hook this into wp_enqueue_scripts
Now, inside your shortcode function, enqueue the files.
wp_enqueue_style('theme-css')
Using this way you can use the shortcode anywhere you want and the script is loaded only when shortcode is present on the page whether it is on content or template.

Wordpress Hook the_content Within Widget

I am writing a widget that list the headings in the post and then created hash links and edits the HTML to reflect that. I've got the list widget content figured out and I just need to edit the_content, i've tried to add a filter for the method that returns the updated code but it's not working.
What would be the best way to do this? My class is called post_headings_widget and the edited HTML content is stored within $this->the_content.
I was hoping I could do this within the widget class
public
function edited_content() {
return $this->the_content;
}
and then to edit the content output here
add_filter( 'the_content', [ 'post_headings_widget', 'edited_content' ] );
It calls the class method fine but i'm not sure exactly how it works so i'm guessing it called the method directly without calling the constructors etc?
I have also tried to just create a filter from within the widget() method but that did not work either, heres what I tried:
add_filter( 'the_content', function() {
return 'test';
} );
Any ideas on a solution?
You have to pass the_content as a parameter in your filter function/callback.
Check the Wordpress docs: https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content
On widgets you need to bind on widget_text
add_filter('widget_text', 'se24265_my_function');
function se24265_my_function( $content )
{
# replace code here on widget $content
return $content;
}

How to filter pluggable function from wordpress child-theme

I need to add a filter to a specific wordpress function which is defined in a file which is included by a pluggable function theme's functions f
functions.php:
if (!function_exists('nectar_colors_css_output')) {
function nectar_colors_css_output(){
include('css/colors.php');
}
}
colors.php:
<?php
function nectar_colors() {
// not relevant what happens here, but I have
// to call another function before this one is called!
}
?>
I use a child theme and when I try to filter this function from the child theme's functions.php, nothing happens. This is because pluggable functions of the parent theme will be called after calling the filters from the child theme.
My filter function in the child theme's functions.php is
function filter_function() {
// some custom actions...
}
add_filter('nectar_colors', 'filter_function');
How can I get this filter working?
What exactly are you trying to filter on? You might be misunderstanding the concepts.
Take your example provided. The parent checks if a function exists before creating it.
So basically, if you define it in your child's theme, the parent will know it's there and won't create a default one.
#themename-child
function nectar_colors_css_output(){
error_log("It worked!");
}

WordPress template_include - how to hook it properly

I'm currently coding a WP plugin and would need to override templates.
My filter hook looks like that - and it executes:
add_filter('template_include', 'mcd_set_template',10);
function mcd_set_template() just returns the required path as string - or the default WP template in case the file not exists.
I'm toying with this for hours already, even could include that alternate template (but it appears at the bottom of the page).
So my question is, how to force WP 3.2.1 to just load another template file instead - and which priority is required??
Update:
Also I noticed when using var_dump ... it outputs almost at the end of the file - but should appear before the opening HTML tag...
According to this ticket it should work with template_include hook: http://core.trac.wordpress.org/ticket/11242
Or is the only way to hook these filters instead:
http://codex.wordpress.org/Template_Hierarchy#Filter_Hierarchy
?
You could use template_redirect as shown above, but that does require exit, and it does trample on everything else WordPress would normally do to find the current template. You may want to let that happen and then apply logic to the current template.
Using some of what is above...
add_action('template_include', 'mcd_set_template');
function mcd_set_template() {
return locate_template('templatename.php');
}
That is fairly simple, you can also pass an array to locate_template() to define a hierarchy. If you were to use 'template_redirect as shown above, you should still be using locate_template, and this is how.
add_action('template_redirect', 'mcd_set_template');
function mcd_set_template() {
/**
* Order of templates in this array reflect their hierarchy.
* You'll want to have fallbacks like index.php in case yours is not found.
*/
$templates = array('templatename.php', 'othertemplate.php', 'index.php');
/**
* The first param will be prefixed to '_template' to create a filter
* The second will be passed to locate_template and loaded.
*/
include( get_query_template('mcd', $templates) );
exit;
}
Finally, the best way would be to filter specific types instead of the whole hierarchy. For example you could filter 'category_template' or 'page_template'. That would be more specific, it would avoid messing with the whole template hierarchy if you don't want to - and it lets WordPress do more of the heavy lifting
For example:
add_filter('category_template', 'filter_category_template');
function filter_category_template($template){
/* Get current category */
$category = get_queried_object();
/* Create hierarchical list of desired templates */
$templates = array (
'category.php',
'custom-category-template.php',
'category-{$category->slug}.php',
'category-{$category->term_id}.php',
'index.php'
);
return locate_template($templates);
}
You can of course create that array of hierarchical templates any time you use locate_template(). Using this method, its easy to see how easily you could create all sorts of very detailed and specific hierarchies either as part of or separate from the native Template Hierarchy.
Have you tried using an add_action instead? For example, you might want to try something like the following in your plugin:
add_action('template_redirect', 'mcd_set_template');
//Redirect to a preferred template.
function mcd_set_template() {
$template_path = TEMPLATEPATH . '/' . "templatename.php";
if(file_exists($template_path)){
include($template_path);
exit;
}
}
Here is a helpful reference: http://www.mihaivalentin.com/wordpress-tutorial-load-the-template-you-want-with-template_redirect/

Resources