Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKristian Woods
23,414 PointsWhy can't I edit the content on the page with the add_filter() function?
I just echo "default value" - I can't seem to update the content on the page.
<?php
function add_my_custom_nav_styles($content){
return str_replace('again', 'TEST', $content);
}
add_filter('change_nav', 'add_my_custom_nav_styles', 10);
echo $myValue = apply_filters('change_nav', 'default value');
?>
including the file in my functions.php
require_once('apply_filters.php');
1 Answer
Stefan Hall
51,970 PointsHi Kristian,
Do you mean that your page is showing up blank? If so it's likely because your require_once path isn't pointing to where the apply_filters.php file is located. If you're using the example theme from the Project Files then apply_filters.php should be located in the "examples" folder.
Try this and see if it works:
require_once( 'examples/apply_filters.php');
or this:
require_once(get_template_directory() . '/examples/apply_filters.php');
Hope this helps!
Kristian Woods
23,414 PointsKristian Woods
23,414 PointsHey Stefan,
I’m not getting a blank page. I’m trying to create a filter that changes the text on the page from “again” to “TEST”. However, the str_replace function doesn’t seem to be working. All I’m echoing to the page is the default value of the filter “default value”. I created my own file. I’m not using the examples folder.
Stefan Hall
51,970 PointsStefan Hall
51,970 PointsAh, then it might be because your $myValue variable is passing the string 'default value' through to str_replace() in your function. str_replace is then looking for the word 'again' in 'default value' and replacing it with 'TEST', but since 'default value' doesn't contain 'again' it's simply returning 'default value' unchanged.
In order to change content on the page, you'll need to pass that content through your function. In other words, your page content to be changed will need to go where you currently have 'default value'.