- This topic has 36 replies, 3 voices, and was last updated 3 years, 3 months ago by David.
-
AuthorPosts
-
November 9, 2020 at 5:30 am #1523704DavidStaffCustomer Support
Glad we could be of help!
December 15, 2020 at 11:06 am #1581927EricHey GP Team, OK so I have another update with this thread… I hope you don’t mind.
I am now finally trying to trigger these PHP filters using post tags like I mentioned a while back but the solution you guys gave to me is not working like we discussed.
So I have a global setting from the first code you guys gave that looks something like this:
function db_change_content_h2( $content ) { $content = str_ireplace( '<h2>', '<h3>', $content ); $content = str_ireplace( '</h2>', '</h3>', $content ); return $content; } add_filter( 'the_content', 'db_change_content_h2' ); function db_change_content_h3( $content ) { $content = str_ireplace( '<h3>', '<h2>', $content ); $content = str_ireplace( '</h3>', '</h2>', $content ); return $content; } add_filter( 'the_content', 'db_change_content_h3' ); function db_change_content_h4( $content ) { $content = str_ireplace( '<h4>', '<h3>', $content ); $content = str_ireplace( '</h4>', '</h3>', $content ); return $content; } add_filter( 'the_content', 'db_change_content_h4' );
This is affecting the entire site replacing the strings:
1) h2 to h3
2) h3 to h2
3) h4 to h3Directly below this global setting function, I have placed the “filter by post tag” code to change the headings differently but only for that specific post tag. This is what I tried to use:
if (has_tag( 'recipes' ) ) { function db_change_content_h2( $content ) { $content = str_ireplace( '<h2>', '<h2><span>', $content ); $content = str_ireplace( '</h2>', '</span></h2>', $content ); return $content; } add_filter( 'the_content', 'db_change_content_h2' ); function db_change_content_h3( $content ) { $content = str_ireplace( '<h3>', '<h2><label>', $content ); $content = str_ireplace( '</h3>', '</label></h2>', $content ); return $content; } add_filter( 'the_content', 'db_change_content_h3' ); function db_change_content_h4( $content ) { $content = str_ireplace( '<h4>', '<h2><summary>', $content ); $content = str_ireplace( '</h4>', '</summary></h2>', $content ); return $content; } add_filter( 'the_content', 'db_change_content_h4' ); function db_change_content_p( $content ) { $content = str_ireplace( '<p>', '<p><span>', $content ); $content = str_ireplace( '</p>', '</span></p>', $content ); return $content; } add_filter( 'the_content', 'db_change_content_p' ); function db_change_content_li( $content ) { $content = str_ireplace( '<li>', '<li><label>', $content ); $content = str_ireplace( '</li>', '</label></li>', $content ); return $content; } add_filter( 'the_content', 'db_change_content_li' ); }
For this tag I wanted to modify and add different tags to each of the specified elements on the page. I’ve got the following:
1) h2 to h2span
2) h3 to h2label
3) h4 to h2summary
4) p to pspan
5) li to lilabelSo the global settings work no problem but I can’t seem to get the tags to trigger the specific filtering. I’ve also tried your initial suggestion which was to use this:
if ( is_singular() && has_term('', 'recipes') ) {
Unfortunately, I’m not really using taxonomy and terms, but only post tags. How I should go about this?
I would like to set several tag triggered templates this way but I am not too sure what limitations this may create on my site. I intend to just assign a tag to a post and then the elements will swap out as indicated but please let me know if it’s even possible to swap out specific tags like this haha, I hope I’m not breaking anything!
Also, is there an issue with placing the global setting and the specific tag settings one after the other in the functions.php file? I am just wondering if one is overriding the other and if there is something else that I should be doing to make them all play nice together.
Thanks so much again for your time and attention with this. I hope you guys have all been staying safe and more power to GP! Hope to hear from you soon!
December 15, 2020 at 4:21 pm #1582188DavidStaffCustomer SupportYou’re IF condition needs to be inside the function not wrapping a group of functions. For example you could combine your Global and has_tag() functions into one:
function db_change_content_h2( $content ) { if (has_tag( 'recipes' ) ) { // If i am recipe string replace this $content = str_ireplace( '<h2>', '<h2><span>', $content ); $content = str_ireplace( '</h2>', '</span></h2>', $content ); } else { // else apply the global string replace $content = str_ireplace( '<h2>', '<h3>', $content ); $content = str_ireplace( '</h2>', '</h3>', $content ); } return $content; } add_filter( 'the_content', 'db_change_content_h2' );
December 15, 2020 at 8:30 pm #1582345EricOk yeah that seems to work! But is there a way to just add them one at a time rather than combining the functions together? Or is it an all or nothing situation? I’m thinking that it’s probably best to keep things as simple as possible just in case I forget how I did it lol! For example, I will have my global template at the top, then I will just add new tag triggered functions one at a time below them. That would then remove the “else” condition, right? Is that possible? This is what I tried but it didn’t work:
function db_change_content_p( $content ) { if (has_tag( 'recipes' ) ) { // If i am recipe string replace this $content = str_ireplace( '<p>', '<p><span>', $content ); $content = str_ireplace( '</p>', '</span></p>', $content ); } return $content; } add_filter( 'the_content', 'db_change_content_p' );
December 16, 2020 at 7:18 am #1583065DavidStaffCustomer SupportYou would need to exclude the other other filters from your global filter for that to work.
function db_change_content_h2( $content ) { if ( !has_tag( 'recipes' ) || !has_tag( 'reviews' ) || !has_tag( 'aliens' ) ) { // I am global but not recipes, reviews or aliens..... } return $content; } add_filter( 'the_content', 'db_change_content_h2' );
I would personally stick with the combined filter it will save those extra function callbacks.
December 16, 2020 at 11:11 am #1583394EricAh good point with the callbacks… OK ill try to get this working! Thanks again!
December 16, 2020 at 4:50 pm #1583627DavidStaffCustomer SupportYou’re welcome
-
AuthorPosts
- You must be logged in to reply to this topic.