$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - add_filter function concatenate string and locate_template function|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - add_filter function concatenate string and locate_template function

matteradmin9PV0评论

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;
}
Post a comment

comment list (0)

  1. No comments so far