Community Forums › Forums › Archived Forums › General Discussion › Keeping the 'Private' prefix on filtered page titles
- This topic has 10 replies, 2 voices, and was last updated 9 years, 8 months ago by GermanKiwi.
-
AuthorPosts
-
August 11, 2014 at 5:15 pm #118298GermanKiwiMember
Hi, I hope someone might help me with a Genesis problem.
I'm trying to figure out if it's possible for me to keep the "Private" and "Protected" prefixes on the applicable page titles, when I am replacing the title text using the "genesis_post_title_text" filter. So I actually *want* to keep these prefixes on my page titles.
Here's what I'm doing:
I've added a custom meta box to my "Edit Page" page, with a field that I'm using to add an alternative page title for certain pages. The value of this field is saved as 'page-heading' in the post meta.
Then I've got the function below which assigns this field to $PageHeading and returns it to the "genesis_post_title_text" filter, if it's not empty.
However, if the page in question is marked as private, the resulting title does *not* get the "Private" prefix added to it. I'm not sure why. I would have thought that my function would merely replace the default title text with the alternative (meta box) title text, but would still apply all other filters or functions to that text, including whatever filter/function adds the "Private" prefix to the text.
Any ideas?
Here's my function:
function custom_page_heading( $title ) { if ( is_page() ) { // Retrieves the stored value from the database $PageHeading = get_post_meta( get_the_ID(), 'page-heading', true ); if( !empty( $PageHeading ) ) return wptexturize( $PageHeading ); // If $PageHeading is empty, then return the regular title return $title; } } add_filter('genesis_post_title_text', 'custom_page_heading' );
August 11, 2014 at 10:30 pm #118328Ren VenturaMemberYou can further customize the titles using the get_post_status() function and some extra conditionals.
http://codex.wordpress.org/Function_Reference/get_post_status
Web & Software Developer & Blogger | RenVentura.com | Follow Me on Twitter @CLE_Ren
August 12, 2014 at 4:12 pm #118465GermanKiwiMemberThanks for that tip!
Okay here's a follow-up question: with my function above, why isn't the $PageHeading variable passed through the "private_title_format" filter after it is returned by my function here? Because if it *was* passed through "private_title_format" then it would surely have the "Private:" prefix added to it, which would solve my problem. Right?
The regular $title variable does get passed through "private_title_format", and my function above is simply changing the contents of $title, so surely that should then get sent through "private_title_format"?
August 12, 2014 at 4:45 pm #118468Ren VenturaMemberIt appears you're returning the $title variable without tying it to the if statement. This means that you're always returning $title to the filter. Add an else before returning the $title:
function custom_page_heading( $title ) { if ( is_page() ) { // Retrieves the stored value from the database $PageHeading = get_post_meta( get_the_ID(), 'page-heading', true ); if( !empty( $PageHeading ) ) { return wptexturize( $PageHeading ); } // If $PageHeading is empty, then return the regular title else return $title; } } add_filter('genesis_post_title_text', 'custom_page_heading' );
Web & Software Developer & Blogger | RenVentura.com | Follow Me on Twitter @CLE_Ren
August 12, 2014 at 6:09 pm #118474GermanKiwiMemberThanks - although I'm not sure that is actually correct. The function actually works fine as it originally was, in terms of displaying the correct text in the page title. Which is to say, if I add my own text to my custom meta box (retrieved as $PageHeading), this text is displayed on the page. And if I *don't* add my own text to the meta box, then the default page title is displayed instead. So the function works even without the "else" statement.
I tried your modification anyway, but that hasn't made any difference in terms of the "Private" prefix being added, which implies that it's still not passing through the "private_title_format" filter. Do you have any ideas about that, per my previous question?
BTW I'll leave my function the way you've modified it, because it looks neater even if the previous way worked anyway. 🙂
August 12, 2014 at 7:10 pm #118478Ren VenturaMemberI initially thought this may have been what you were talking about but, after re-reading it, I then thought you were having trouble getting the value of $PageHeading to display. Since you're filtering the content of genesis_post_title_text, you should be able to solve this by concatenating the word you want to display onto your $PageHeading variable and returning it under the proper conditionals (i.e. get_post_status).
Web & Software Developer & Blogger | RenVentura.com | Follow Me on Twitter @CLE_Ren
August 12, 2014 at 7:48 pm #118482GermanKiwiMemberOkay, I think I understand. I guess what I'm wanting to know, though, is why the title text (from $PageHeading) doesn't get passed through the "private_title_format" and "protected_title_format" filters. Surely that would cause the appropriate prefixes to get added to the title text?
Is it a matter of priority, ie. is my function perhaps firing *after* those two filters have already filtered the original title text? And therefore, when the title text gets replaced with the contents of $PageHeading, the new text won't get passed through those two filters again? (Sorry if my understanding of how this works is incorrect).
August 12, 2014 at 11:51 pm #118515Ren VenturaMemberWell, using the WordPress filters does not mean that "Private" and "Protected" would automatically be added because you're filtering the entire title. Since you are not using the default $title in the genesis_post_title_text filter but rather returning something completely new, you'll need to account for the prepended words normally included by WordPress (Private and Protected). That said, you can probably use whichever filters you want; I would assume that you could use the general WordPress hooks or the Genesis specific hook with conditionals to achieve the same results. For example, the two following snippets should be equivalent (I've modified your snippet somewhat):
add_filter( 'private_title_format', 'custom_page_heading' ); function custom_page_heading( $format ) { if ( is_page() ) { $PageHeading = get_post_meta( get_the_ID(), 'page-heading', true ); if( !empty( $PageHeading ) ) { return 'Private: ' . wptexturize( $PageHeading ); } } }
add_filter( 'genesis_post_title_text', 'custom_page_heading' );
function custom_page_heading( $title ) {
if ( is_page() ) {
$PageHeading = get_post_meta( get_the_ID(), 'page-heading', true );
if( !empty( $PageHeading ) && get_post_status () == 'private' ) {
return 'Private: ' . wptexturize( $PageHeading );
}
}
}
`I stand to be corrected if I'm wrong on this.
Web & Software Developer & Blogger | RenVentura.com | Follow Me on Twitter @CLE_Ren
August 13, 2014 at 10:22 am #118596GermanKiwiMemberOkay, it all makes more sense to me know. I think the best way is to use 'get_post_status' as per your 2nd example above.
The only remaining issue there, is that 'get_post_status' does not return any unique value for password-protected pages - which I find rather odd! It covers every other status type except that. Which means I can't use it to prepend "Protected" to the title for applicable pages. I'll have to search for another solution for those pages I guess.
August 13, 2014 at 12:16 pm #118614Ren VenturaMemberThis may be of some help:
http://codex.wordpress.org/Function_Reference/post_password_required
Web & Software Developer & Blogger | RenVentura.com | Follow Me on Twitter @CLE_Ren
August 14, 2014 at 7:05 am #118766GermanKiwiMemberYeah I saw that one yesterday too. Unfortunately it doesn't quite help because it will not return false anymore once the user has entered in the password. In other words, I could only use it to display a prefix on the title *before* the user entered in the password - after that point, the prefix would no longer be displayed - which is different to the behaviour of WordPress with the regular title.
I have found another solution that seems to work though, for password-protected pages, whether the password has been entered or not:
$post = get_post(); if( !empty( $post->post_password ) ) { >code for adding a prefix to my custom title< }
In fact I believe this is the code used by the original 'private_title_format' filter too. I guess this is the best way to do it, in lieu of ‘get_post_status’ supporting password-protected posts.
Thanks for all your help!
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.