Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

%%parent_title%% doesn't work on pages with no parent #985

Closed
GermanKiwi opened this issue Apr 4, 2014 · 13 comments
Closed

%%parent_title%% doesn't work on pages with no parent #985

GermanKiwi opened this issue Apr 4, 2014 · 13 comments

Comments

@GermanKiwi
Copy link

I've just added the following to the Pages title template at SEO -> Titles & Metas:

%%title%% %%page%% %%sep%% %%parent_title%% %%sep%% %%sitename%%

It works fine for my pages which do have a parent page, but for my pages that have no parent, their titles look like this:

Kittens » %%parent_title%% » My Site Name

...ie. the %%parent_title%% shortcode stays as-is.

I'd really like to use this shortcode in the Pages title template, so that it automatically applies to all appropriate pages without requiring any further work from me (which is the main advantage of these templates!) - rather than having to manually enter it into the "SEO Title" field of the WP-SEO meta box on each individual page, which would obviously be a nuisance and would somewhat defeat the purpose of having a nice shortcode in the first place.

Could this shortcode therefore be updated, please, so that it hides itself when no parent page exists?

It would also need to be able to hide the preceding (or following?) separator too - otherwise pages with no parent would have two separators right next to each other.

Cheers!

@GermanKiwi
Copy link
Author

...Regarding hiding the preceding or following separator - one idea would be to include them in brackets (or similar) with some kind of "if exists" conditional behaviour that only outputs the mentioned shortcodes/text if the condition matches:

[ifexists (%%parent_title%%), "%%sep%% %%parent_title%%"]

Something along those lines. That would also allow me to include other text in the condition too - or in my own case, a hard-coded separator (I use » instead of %%sep%% in my title templates, because my theme doesn't allow me to specify a separator which can be picked up by the %%sep%% shortcode).

@difreo
Copy link

difreo commented Jun 24, 2014

Would be nice to have this resolved. As a temporary fix one can change the wpseo-function.php (not recommended) line 363 adding something like:
else { $string = str_replace( '%%parent_title%% -', '', $string ); }

@jrfnl
Copy link
Contributor

jrfnl commented Jun 24, 2014

@GermanKiwi @difreo The whole replace functionality has been rewritten/refactored now. This issue should be solved now (non-replaced string being replaced with empty and potential duplicate separators being removed). Regarding the extra text: you can always filter the title to do extra things like that yourself.
If you are willing and able, if would be great if you could test the refactor. The changes are available in the master branch of this github repo.

Ref: #1172

@GermanKiwi
Copy link
Author

Thanks @jrfnl - I've installed the master branch file and I can confirm it works fine - the duplicate separator is removed when there is no parent title.

The only remaining issue for me here, is that I need to be able to change the separator character. I'm using Genesis, and as you know the Genesis SEO options get hidden when the WordPress SEO plugin is installed - and that includes the Genesis option for changing the separator character! So there is no way for me to redefine what the %%sep%% variable equates to. So until now I've had to manually replace the %%sep%% string in all of my title templates, with a hard-coded separator (in my case I'm using '»').

But obviously that won't work with %%parent_title%% and your new fix here. It will result in duplicate separators for any page that doesn't have a parent title.

The proposal shown in the screenshot at #981 would be a perfect solution - ie. creating a new setting on the Titles & Metas page, which lets me choose which separator character I want to use. Will this proposal be implemented any time soon? It would save the day for me!

@jrfnl
Copy link
Contributor

jrfnl commented Jun 25, 2014

@GermanKiwi I've added a 'wpseo_replacements_filter_sep' filter in an open change to the refactor (#1250). I expect that will be merged before the next release. You like ? ;-)

That should solve your issue until a decision has been made about #981.

@jrfnl jrfnl closed this as completed Jun 25, 2014
@GermanKiwi
Copy link
Author

Thanks @jrfnl - that new filter sounds good. Can I use it already now, or do I have to wait for it to be merged into the release version of the plugin at wordpress.org? This new filter doesn't seem to exist right now in the master branch file you linked to above.

I'm also unsure how to actually use it - is there an example function or documentation I can view to see how it works?

Hopefully #981 will be added eventually - that will be the best solution! :)

@jrfnl
Copy link
Contributor

jrfnl commented Jun 26, 2014

@GermanKiwi The filter is part of an open pull request, so it's not in the master yet, nor in the version at wordpress.org.
Even so, you can already add the code to use it to your (child-)theme's functions.php file as it will be ignored while the filter is not yet in place, but once the code has been merged and you've updated your install with the newest WPSEO which contains it, it will magically start working. Isn't that nice ?

Example on how to use it:

function kiwis_title_separator( $sep ) {
    return '»';
}
add_filter( 'wpseo_replacements_filter_sep', 'kiwis_title_separator' );

@jrfnl
Copy link
Contributor

jrfnl commented Jun 26, 2014

Oh and if you want something that should already work with the current master version (not the latest official release), try:

function kiwis_title_separator( $replacements ) {
    if ( isset( $replacements['sep'] ) ) {
        $replacements['sep'] = '»';
    }
    return $replacements;
}
add_filter( 'wpseo_replacements', 'kiwis_title_separator' );

@GermanKiwi
Copy link
Author

Thanks again @jrfnl - however, I can't get that last function (using wpseo_replacements) to work!

I've downloaded and installed the latest master.zip file just now (to achieve this, I removed the existing "wordpress-seo" subfolder from my plugins folder, then I renamed the master.zip copy to "wordpress-seo" and uploaded that to the plugins folder).

Then I pasted in your function above, verbatim - didn't change anything. (And I'm not a functions noob either ;) - I've got a bunch of other functions in my functions.php file). But the %%sep%% variable is still resolving to a dash character, and not the '»' character that is specified in the function.

Do I need to add some particular priority to this function, maybe? Any other ideas?

@jrfnl
Copy link
Contributor

jrfnl commented Jun 26, 2014

My apologies, posted too quickly, it should of course be:

function kiwis_title_separator( $replacements ) {
    if ( isset( $replacements['%%sep%%'] ) ) {
        $replacements['%%sep%%'] = '»';
    }
    return $replacements;
}
add_filter( 'wpseo_replacements', 'kiwis_title_separator' );

Oh and just make sure that if you use a Unicode character for the separator, that you save the file in the right encoding.

@GermanKiwi
Copy link
Author

Okay we're getting there! :) That updated function does the trick - now my separator symbol is » rather than a dash.

But this seems to introduce a new problem (or rather, the original problem) - when this function is active, the titles of my pages which do not have a parent page now show a double separator character. In other words, the fix you created to remove the 2nd separator when there is no parent page, doesn't seem to work when this function is active.

Example: my Pages title template looks like this:

%%title%% %%page%% %%sep%% %%parent_title%% %%sep%% %%sitename%%

With your function active, the title of one of my pages - which has no parent - is this:

Foobar » » My Site Name

Note the double '»'. But when I disable your function, the page title is this:

Foobar - My Site Name

(ie. the separator is now the default dash character, and it correctly hides the 2nd separator).

Good luck! :)

@jrfnl
Copy link
Contributor

jrfnl commented Jun 26, 2014

@GermanKiwi You're completely right. I hadn't taken the filter into account when writing the undoubling code. Fixed this now and committed to master. Thanks for keeping me on my toes ;-)

@GermanKiwi
Copy link
Author

Perfect - all working great now! Thanks so much!

herregroen pushed a commit that referenced this issue Apr 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants