Using Shorcodes in Sharing Preview and Notifications Title from Post Title - php

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.

Related

Simple wordpress shortcode function

I have a little piece of code that makes a shortcode in my WordPress theme function file.
This is the code:
function myshort_title($atts){
$title = get_the_title();
$short_title = wp_trim_words( $title, 2, '' );
return $short_title;
}
add_shortcode( 'short_title', 'myshort_title' );
and I use it like this:
[short_title]
Ok...what I want is the result of that shortcode to be written permanently into my WordPress posts so that when I edit my posts I don't want to see that shortcode, I want to see the result.

Custom Shortcodes in Yoast SEO

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

Use wordpress shortcode in post title

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 );
?>

Add login form shortcode programatically to every published product page

I am running a wholesale shop on Woocommerce. Login is required to see the prices. This is set up and working properly. Now I wish to add a logon form on every product page to only show to visitors (not logged on users).
I am using the WooCommerce Catalog Visibility plugin. This plugin offers the functionality I described above, but my theme is somehow messing it up. The plugin author says to talk to the theme developer and the theme developer says to talk to the plugin author. So now I am trying to find a workaround.
First issue: The plugin comes with a shortcode [woocommerce_logon_form] that will display a logon form. I don't want to manually add this to every existing product since I have thousands of products on my site. I am looking for a way to get it in through the code for the product page layout.
I found this code (to be added to the functions.php) to work well:
// adds notice at single product page above add to cart
add_action( 'woocommerce_single_product_summary', 'return_policy', 20 );
function return_policy() {
echo '<p id="rtrn">30-day return policy offered. See Terms and Conditions for details.</p>';
}
However, it will only show text. The short code won't work when added instead of the sample text.
Second issue: The short code shows the form even when the customer is already logged in.
I am currently using this nice code that shows or hides content depending on whether the user is logged in or not:
add_shortcode( 'access', 'access_check_shortcode' );
function access_check_shortcode( $attr, $content = null ) {
extract( shortcode_atts( array( 'capability' => 'read' ), $attr ) );
if ( current_user_can( $capability ) && !is_null( $content ) && !is_feed() )
return $content;
return '';
}
add_shortcode( 'visitor', 'visitor_check_shortcode' );
function visitor_check_shortcode( $atts, $content = null ) {
if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
return $content;
return '';
}
That shortcode works perfectly for text, but not with other shortcodes.
So the combination of these short codes: [visitor][woocommerce_logon_form][/visitor] will not show the logon form to visitors. Instead it will only show them this as text [woocommerce_logon_form].
Please help! I am sure this is probably easily fixed by someone with coding skills.
I appreciate your effort to answer to this question. Keep in mind that my understanding of code is very limited and it would be great if you can also point out in which file to add or modify code.
To make your shortcode working in php code or in php/html code you need to use a native WordPress function do_shortcode() … You can use it with your shortcode for example in your 1st function this way:
add_action( 'woocommerce_single_product_summary', 'return_policy', 20 );
function return_policy() {
echo do_shortcode('[woocommerce_logon_form]');
}
And this will work…
To see all the different hooks you can use instead of woocommerce_single_product_summary, please see this 2 templates code to chose in case a more convenient hook:
WooCommerce single-product.php template
WooCommerce content-single-product.php template
You can also add it the same way in one of your existing short codes, this way:
add_shortcode( 'visitor', 'visitor_check_shortcode' );
function visitor_check_shortcode( $atts, $content = null ) {
if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
return do_shortcode('[woocommerce_logon_form]');
return '';
}
And this will work too.
See as reference this answer: Change markup in WooCommerce shortcode output
So as you can see your problem is solved on both issues

Use "do_shortcode" in a WordPress plugin is not working

I have this plugin installed on my WordPress:
http://wordpress.org/plugins/put/
I’m trying to make a plugin that uses the UI Tabs plugin inside my own plugin.
My plugin code so far:
function load_jquery(){
echo '<link rel=\'stylesheet\' id=\'jquery-ui-tabs-css\' href=\'http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/themes/smoothness/jquery-ui.css?ver=1.9.2\' type=\'text/css\' media=\'all\' />';
}
add_action('wp_head','load_jquery');
function print_tabs(){
echo do_shortcode('[tab name="Tab"]-[/tab]');
echo do_shortcode('[end_tabset]');
}
add_shortcode('print_tabs', 'print_tabs');
Now if I use the [print_tabs] shortcode in a new page, it should look like this:
http://img835.imageshack.us/img835/4905/workingp.png
But it’s not working, and it looks like this:
http://imageshack.us/a/img62/9772/notworkingm.png
What could be the problem here?
The problem, from what I can see in put.php in the Post UI Tabs plugin is that the shortcodes are only added during the "the_content" filter in a function called "on_the_content".
add_filter( 'the_content', array( $this, 'on_the_content' ), 7 ); // Priority 7 - before wpautop
(line 96 of put.php)
And that function looks like:
public function on_the_content( $content ) {
$this->has_tabs = (bool) apply_filters( 'put_decide_has_tabs', $this->has_tabs );
if( !$this->has_tabs )
return $content;
global $shortcode_tags;
// Backup current registered shortcodes and clear them all out
$orig_shortcode_tags = $shortcode_tags;
$shortcode_tags = array();
add_shortcode( 'tab', array( $this, 'on_tab_shortcode' ) );
add_shortcode( 'end_tabset', array( $this, 'on_tab_end_shortcode' ) );
// Do the shortcode(only the tab shortcode is registered at this point)
$content = do_shortcode( $content );
// Put the original shortcodes back
$shortcode_tags = $orig_shortcode_tags;
return $content;
}
(starting at line 118 of put.php)
So, given how this plugin is written by modifying the content with a filter which in turn adds the shortcodes when that filter is run, what you're seeing is probably happening because when you call "do_shortcode" those shortcodes don't actually exist.
What echoing do_shortcode is doing then, is just coughing up the text.
Unfortunately, because of the way the Post UI Tabs plugin is written, you may not be able to do what you're trying to do.

Categories