changing title to page in get_header()
-
Is there a way to change the
<title>title</title>
of a page that I’m creating with this code?<?php require_once($_SERVER["DOCUMENT_ROOT"].'/wp/wp-load.php'); get_header(); ?>
Thank you
-
Look at wp_title(). It is filtered, and there are versions for RSS also.
Thank you for your suggestion @joyously however I am looking for a way to alter the
<title>
tag element in the HTML output ofget_header();
and from what I can see on thewp_title()
link you sent that function doesn’t let me do that, unless I’m missing something…Yes, you are missing something. In the old days, themes would call
wp_title()
and it was a mess with SEO plugins trying to fix things. So they added a filter and told the theme not to call it directly, and to indicate that by callingadd_theme_support('title-tag')
. When the theme does that, core will callwp_title()
with consistent parameters, and the plugins can change things with the filter.
So if you want to change the title, use thewp_title
filter, but you might not be the only one doing that.
I’m guessing you don’t care about the RSS title since you are loading WP yourself.Apologies but I am not sure what you mean by “filter” (I’m not a professional coder, sorry)… can you give me an example of how I would change the page title before calling
get_header()
?I’ve tried things like:
get_header(wp_title('test'));
but to no avail…One the page I linked, the first one is the wp_title function itself, and you can read the code to see what it does. Part of what it does is
apply_filters
the result.
The second one on the page is the filter, and it shows you what parameters will be passed. Filters can only affect the first parameter, and they must return it (or all WP breaks). There is more information on that page.To use a filter, you write a function to change what you expect to what you want. Then you add your function to the list of filters to apply when the time comes for that.
function myown_wp_title_filter($title, $sep, $seplocation) { // check for context, or always change it if ( is_home() ) { $title = 'HOME!'; } return $title; } add_filter( 'wp_title', 'myown_wp_title_filter', 10, 3 );
-
This reply was modified 4 years, 10 months ago by
Joy. Reason: used wrong quote mark
Thank you for the explanation @joyously but I still don’t understand how I can then pass the title to the get_header() function that I call on a non-WP page…
The code I’m using to test it is this:
require_once($_SERVER["DOCUMENT_ROOT"].'/wp/wp-load.php'); $title = "TEST"; function myown_wp_title_filter($title, $sep, $seplocation) { return $title; } add_filter( $title, 'myown_wp_title_filter', 10, 3 ); get_header()
-
This reply was modified 4 years, 10 months ago by
marcnyc.
Right, you messed up the code I gave you.
Use my code, but only change what is inside the function.
The trick about filters is that you aren’t passing the data around. WordPress is.
Your code is for some filter named ‘TEST’, that no code is calling.
get_header
will call the theme’s header template, which will callwp_title
, which will filter the title through whatever functions were added to the list forwp_title
.Apologies for messing up…
Ok I’ve copied your code exactly and only added theelse
inside the function:require_once($_SERVER["DOCUMENT_ROOT"].'/wp/wp-load.php'); function myown_wp_title_filter($title, $sep, $seplocation) { // check for context, or always change it if ( is_home() ) { $title = 'HOME!'; } else { $title = 'TEST'; } return $title; } add_filter( 'wp_title', 'myown_wp_title_filter', 10, 3 ); get_header();
But I still don’t see “TEST” (or “HOME!”) in the title.
So now that you have correct WP code, do you have a theme and is its header.php file either calling wp_title or is it using add_theme_support(‘title-tag’) in functions.php?
I do not have a theme. As I mentioned I am trying to call the header from an external non-WP page, on the same domain as the WP installation, but not a page generated by WP.
Basically all I have in my page is the code you saw above and eventually the full code will be this:<? require_once($_SERVER["DOCUMENT_ROOT"].'/wp/wp-load.php'); function myown_wp_title_filter($title, $sep, $seplocation) { // check for context, or always change it if ( is_home() ) { $title = 'HOME!'; } else { $title = 'TEST'; } return $title; } add_filter( 'wp_title', 'myown_wp_title_filter', 10, 3 ); get_header(); echo 'my content here'; get_footer() ?>
“I do not have a theme.”
Yes, but you are loading WP, to get the header, which is generated by the theme.
I’m not sure what WP does when loaded that way, because thedefine('WP_USE_THEMES', true);
line is in the root index.php file. And you aren’t callingwp()
like in wp-blog-header.php.I see… sorry for misunderstanding…
I’m using Newspaper X as my theme on WordPress but I don’t know what functions their theme call…You might have a lot better luck putting your external page content into a page in WP than trying to pull the header and footer out to your page.
Edit: Or don’t make it dynamic. Pull up a page in WP and copy/paste the HTML for header and footer into your external page (or somewhere you can pull it in).-
This reply was modified 4 years, 10 months ago by
Joy.
I cannot do that… it’s not just a page, it’s a whole script with queries and database etc…
I was always under the understanding that the get_header() and get_footer() functions are a perfectly safe way to wrap external content into WP skin and it works beautifully, except for the inability to change the title of the pages, so every page always has the title of the site… that’s why I was here exploring whether this could be done at allWell I didn’t think your content was just a page. You can call other code from within your WP page, though. You either make a template file and do whatever code you want in there, or make a little plugin that provides a shortcode, which calls the external code and returns the content, or put an iframe into your WP page.
If you have the header working except for the title, I would be surprised. The way you called it, a lot of stuff gets loaded, but
wp()
isn’t called, so it seems as if it wouldn’t work right, but then I’ve not looked too deeply into the loading process, and it takes different paths for normal requests, ajax, REST, and cron. -
This reply was modified 4 years, 10 months ago by
- The topic ‘changing title to page in get_header()’ is closed to new replies.