I would like to use custom shortcodes in Yoast SEO Plugin, but I can't bring it to work. I want to put custom time specifications in the meta title.
This is my shortcode:
function time_yoast_shortcode() {
$year = date('Y');
return $year;
}
add_shortcode('yyyy', 'time_yoast_shortcode')
This is how I want the title in the Yoast to look like: THIS IS MY EXAMPLE POST in the year [yyyy]
Any ideas how I can make my shortcode work in Yoast?
For anyone looking to add shortcode support for Yoast SEO's Meta Title, here's how I handled it. This code adds full shortcode support for Meta Title and adds a custom shortcode [year] to output the current year. Add this code to functions.php:
// Add shortcode support to Yoast Meta Title using 'wpseo_title' filter
add_filter('wpseo_title', 'filter_wpseo_title');
function filter_wpseo_title($title) {
$title = do_shortcode($title);
return $title;
}
// Add [year] shortcode (outputs current date in YYYY format)
add_shortcode('year', 'year_shortcode');
function year_shortcode() {
$year = date('Y');
return $year;
}
If you'd like your post to show the date at the end of your SEO Title of Yoast try: %%date%% within the SEO Title field of the yoast plugin on that particular post. For just the year use %%currentyear%%.
In the event you'd like to do this globally you can do this within the post titles & metas section of the plugin settings.
Here is a full list of their varables
We've created a WordPress plugin to do/run/show shortcodes for Yoast SEO. It is currently in version 2.1.1. Unfortunately we cannot upload it to the WordPress repository due to possible copyright infringement (which is not true). So you may download, use parts of our code and use install the whole plugin directly from here:
https://denra.com/wordpress/plugins/do-shortcodes-for-yoast-seo/
We will continue updating the plugin with new features when needed but you may need to keep the URL to the plugin page on our website because we cannot upload to WordPress.org
Add this code in functions.php file and it will work:
// For adding [year] shortcode in WordPress Posts
add_shortcode( 'year', 'sc_year' );
function sc_year(){
return date( 'Y' );
}
add_filter( 'single_post_title', 'my_shortcode_title' );
add_filter( 'the_title', 'my_shortcode_title' );
function my_shortcode_title( $title ){
return do_shortcode( $title );
}
add_filter( 'wpseo_title', 'do_shortcode' );
add_filter( 'wpseo_metadesc', 'do_shortcode' );
add_filter( 'the_title', 'do_shortcode' );
add_shortcode( 'year' , 'current_year' );
function current_year() {
$year = date("Y");
return "$year";
}
Now you can use [year] in a title to post current year.
i am writing this code by considering you have yoast seo plugin
Related
I used some do_shortcode functions to work shortcode in post title and description. but i am unable to use shortcode in notification post title or sharing preview title. Here you can see my sample posts.
I want to add automatic date in my WordPress posts and posts title. I used Following codes to adding current date in WordPress posts.
add_filter( 'wpseo_title', 'do_shortcode' ); // activate shortcode in Yoast Title
add_filter( 'wpseo_metadesc', 'do_shortcode' ); // activate shortcode in Yoast Meta Description
add_filter( 'the_title', 'do_shortcode' ); // activate shortcode in WP Title
add_shortcode( 'current_date', 'mth_footer_date2' );
function mth_footer_date2() {
ob_start();
echo date("l, jS F Y");
return ob_get_clean();
}
Above code is working fine.. but I unable to see date in Sharing preview or Notification Title. Kindly provide solution.
On my competitor's website, I seen that they also adding current date using shortcode. They use another code to done this. on those website, date appearing in both sharing preview and notifications. But, when I used that, I getting some errors. Here is that code.
function wpb_date_today($atts, $content = null) {
extract( shortcode_atts( array(
'format' => ''
), $atts ) );
if ($atts['format'] == '') {
$date_time .= date(get_option('date_format'));
} else {
$date_time .= date($atts['format']);
}
return $date_time;
}
add_shortcode('date-today','wpb_date_today');
They use [date-today] to represent current date everywhere.
In WordPress, you can do the following to edit the content of an article before the page is loaded:
add_filter('the_content', 'edit_content');
function edit_content($content) {
// edit $content
return $content;
}
How can I achieve the same thing but for comments?
You add a filter to the comment_text hook, similar to how you do it with the_content hook. This hook lets you change the comment text that is displayed using comment_text() in your template.
For example:
add_filter( 'comment_text', 'edit_comment_text', 99);
function edit_comment_text( $comment_text, $commentObject, $args ) {
// edit the text....
return $comment_text;
}
Note that you might need to set the priority in add_filter to a high number so that it runs after WP's own filters first - I used 99 above.
You can see in the WP Developer Code Reference for comment_text that the 3 parameters the filter gets are:
$comment_text (string) - Text of the current comment.
$commentObject (WP_Comment|null) - The comment object. Null if not found.
$args (array) - An array of arguments.
I am trying to use the following shortcode in the wordpress post title. The shortcode looks like the following:
//Use [year] in your posts.
function year_shortcode() {
$year = date('Y');
return $year;
}
add_shortcode('year', 'year_shortcode');
Any suggestions how to execute this shortcode in the post title?
I appreciate your replies!
You can absolutely use a shortcode in a title. You just need to use the WordPress hooks system to run the shortcode when the title is called. So if you want to have a shortcode [year] that spits out the current year, you'll create the shortcode:
add_shortcode( 'year', 'sc_year' );
function sc_year(){
return date( 'Y' );
}
Then, hook into the filter for the_title() to run your shortcode:
add_filter( 'the_title', 'my_shortcode_title' );
function my_shortcode_title( $title ){
return do_shortcode( $title );
}
That takes care of the Post/Page title, but you'll also want to run it for the single_post_title hook which is used in wp_head on the title tag on your site. That way, the browser will show the proper title as well:
add_filter( 'single_post_title', 'my_shortcode_title' );
Note: You don't need a separate function here because it's running the exact same code. So your total code would look something like this:
add_shortcode( 'year', 'sc_year' );
function sc_year(){
return date( 'Y' );
}
add_filter( 'single_post_title', 'my_shortcode_title' );
add_filter( 'the_title', 'my_shortcode_title' );
function my_shortcode_title( $title ){
return do_shortcode( $title );
}
I don't think you can apply (save in admin post edit screen) a shortcode in the post title with a shortcode. The post_title is sanitize to avoid tag or shortcode, the post title is used by many function that a shortcode can break.
To make modification on the post_title, you can use the filter the_title
add_filter('the _title', 'yourfunction');
function yourfunction($title){
global $post;
// if you set a custom field on the post where you want to display the year
if(get_post_meta($post->ID, 'display_year', true) == 1){
$title = $title. ' '. date('Y');
}
return $title;
}
Hope it helps
Please add this code in 'function.php'. Try this.
<?php
function TitleFunction($title)
{
global $post;
$title = $title. ' ' .get_the_date('Y');
return $title;
}
add_filter( 'the_title', 'TitleFunction', 10, 2 );
?>
I would like to setup different META syntax on my Wordpress website, depending on the template I use (5 templates for pages and 8 templates for articles.
I use SEO by yoast that only gives the possibility to add ONE META syntax for all pages oand one for ALL articles.
I tried add use this on my function.php for one template (example)
$pref_nom = get_post_meta($post->ID, $pref_nom, true);
function prefecture_filter_wp_title( $title ) {
if ( is_page_template( 'pages-prefectures.php' ) ) {
return $pref_nom;
}
return $title;
}
add_filter( 'wp_title', 'prefecture_filter_wp_title' );
But I doesn't work (notice that $pref_nom is not declared)
What's wrong ?
Thanks in advance for your help.
I've just installed this plugin on my site and it means that my custom titles that were previously set no longer work. Here's the link to the brief Wordpress SEO API doc: [http://yoast.com/wordpress/seo/api-docs/]
Previously, I did this:
$pagetitle = '' . $design['name'] . ' | Free Design from My Site';
I have a custom URL rewrite that takes a WP page /design/ and appends the design name to /design/design-name so now I've installed WP SEO, the page title is whatever the title of /design/ is and isn't specific.
According to the docs in the link above, I tried this:
function wpseo_design_detail_title($pagetitle) {
$pagetitle = $design['name'] . ' | Free Web Template from My Site';
}
add_filter( 'wpseo_title', 'wpseo_design_detail_title' );
That does remove the previous /design/ title but instead it just has the sites URL (i.e. the page title is blank).
I'm probably missing something really simple, right?
This is how it's done:
function YourFunctionName( $title ) {
$title = 'Your Webpage Title';
return $title;
}
add_filter( 'wpseo_title', 'YourFunctionName' );
But #user1502679 suggested, you can use the WordPress' wp_title filter (but it only works if WordPress SEO by Yoast is disabled):
function YourFunctionName( $title, $sep ) {
$title = 'Your Webpage Title';
return $title;
}
add_filter( 'wp_title', 'YourFunctionName' );
why you use Yoast? just add a filter to wp_title