Community Forums › Forums › Archived Forums › General Discussion › How to dynamically set document title from content
- This topic has 5 replies, 2 voices, and was last updated 6 years, 8 months ago by ira1974.
-
AuthorPosts
-
July 10, 2017 at 8:21 pm #208943ira1974Member
I wrote a plugin which writes the page content from an external database. I would like to set the document title based on a data element in that content.
I can set a new document title with the following. However it fires before genesis_entry_content:
remove_filter( 'wp_title', 'genesis_default_title', 10, 3 ); //Default title
remove_action( 'genesis_site_title', 'genesis_seo_site_title' ); //Genesis Extra Stuff
remove_action( 'genesis_site_description', 'genesis_seo_site_description' ); //Genesis Extra Stuffadd_filter( 'wp_title', 'genesis_default_title_new', 10, 3 );
function genesis_default_title_new( $title) {$title = 'Hello World!';
return $title;
}Is it possible to change wp_title after genesis_entry_content?
July 11, 2017 at 6:02 am #208959Victor FontModeratorChange the priority of the filter or action so it fires after the content.
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?July 11, 2017 at 7:06 am #208961ira1974MemberThank you for the reply. How do I determine what priority I should use?
July 11, 2017 at 8:13 am #208964ira1974MemberI am really at a loss here. I have defined a constant in my plugin's controller:
define("MYDOCTITLE","this is my new doc title");
I can echo it in my view:
echo constant("MYDOCTITLE");
But no matter what priority I set the new filter, it does not fire after my content:
remove_filter( 'wp_title', 'genesis_default_title', 10, 3 );
remove_action( 'genesis_site_title', 'genesis_seo_site_title' );
remove_action( 'genesis_site_description', 'genesis_seo_site_description' );add_filter( 'wp_title', 'genesis_default_title_new', 999, 3 );
function genesis_default_title_new() {$title = constant("MYDOCTITLE");
return $title;}
Error Message:
Warning: constant(): Couldn't find constant MYDOCTITLE
July 11, 2017 at 2:20 pm #208995Victor FontModeratorYou said you were getting data from an external database, not a constant. When using constants, you just have to echo them.
I've been thinking more about what you want to do. The title tag is written to the document header way before the content loop. By the time you access your content and retrieve the custom field, the header has already been generated. If you want to access a custom field that is embedded in content, you're going to have to access that content in the callback function you assigned to the wp_title filter.
You also have an error in your custom code. The priority for the wp_title filter should stay at the default of 10. Also the $accepted_args value in your add_filter is 3. With add_filter, the $accepted_args value must reflect the number of arguments the bound callback actually opts to accept. You are not receiving any arguments in genesis_default_title_new(). Your either have to change the function to receive three parameters, or change the add_filter to drop the $accepted_args.
Regards,
Victor
https://victorfont.com/
Call us toll free: 844-VIC-FONT (842-3668)
Have you requested your free website audit yet?July 11, 2017 at 2:42 pm #208998ira1974MemberThank you again for your response. I am fairly new to WP development and your answers are certainly helping me gain a better understanding of its nuances.
Just for clarification I was simply using a constant to test. I've been using custom query_vars within my plugin to pass data around. I removed it here to eliminate that as a point of failure.
I believe you are correct in that the header has already been written.
Therefore my workaround is to simply pass the data I want to use in the request. Not the most elegant solution but should suffice for now.
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.