php - add_filter function concatenate string and locate_template function

admin2025-06-02  0

I am trying to inject a small php component to the end of my main site navigation.

I have written this filter:

add_filter('wp_nav_menu_items', 'add_css_switcher', 10, 2);
function add_css_switcher($items, $args){
    $items .= '<li class="css-toggler hidden">' . locate_template('inc/css-switcher.php', true, true) . '</li>';
    return $items;
}

Currently it outputs the locate_template file url inside the <li> element.

How can I parse in php via the concatenator? Is this possible, I tried wrapping the component in php tags, but this did not work.

TIA

I am trying to inject a small php component to the end of my main site navigation.

I have written this filter:

add_filter('wp_nav_menu_items', 'add_css_switcher', 10, 2);
function add_css_switcher($items, $args){
    $items .= '<li class="css-toggler hidden">' . locate_template('inc/css-switcher.php', true, true) . '</li>';
    return $items;
}

Currently it outputs the locate_template file url inside the <li> element.

How can I parse in php via the concatenator? Is this possible, I tried wrapping the component in php tags, but this did not work.

TIA

Share Improve this question asked Mar 10, 2019 at 12:13 lharbylharby 1034 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Running output when concatenating a string does not work in PHP. If you try this:

function goes() {
    echo 'goes';
}

$string = 'My string ' . goes() . 'here';

echo $string;

Then the output would be:

goesMy string here

The same thing applies for include or require when the files being included produce output.

If you want to concatenate the output of a PHP file into a string, you're going to need to include it and capture the output with output buffering. If the file's in your theme then you should include it with get_template_part(), rather than locate_template().

add_filter('wp_nav_menu_items', 'add_css_switcher', 10, 2);
function add_css_switcher($items, $args){
    ob_start();

    get_template_part( 'inc/css-switcher' );

    $css_switcher = ob_get_clean();

    $items .= '<li class="css-toggler hidden">' . $css_switcher . '</li>';

    return $items;
}

But this is a frankly silly way to do this. If you want to return a value that's meant to be concatenated into a string then a template file is not a smart way to go about it. You should be putting the functionality of css-switcher.php into a function and changing it to return its output, rather than echoing it. Then you can use that function in your code:

function css_switcher() {
    // Return menu item code here.
}

add_filter('wp_nav_menu_items', 'add_css_switcher', 10, 2);
function add_css_switcher($items, $args){    
    $items .= '<li class="css-toggler hidden">' . css_switcher() . '</li>';

    return $items;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748823997a314024.html

最新回复(0)