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.
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.
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'm working on a WordPress site that has been using a plugin to grab amazon product images using a shortcode.
You simply insert the asin of a book into a shortcode, like this:
[amazon template=image&asin=B00FYY53B8]
When the post loads, the shortcode is converted into the actual image HTML using the URL from amazon.... so the example above would return
http://ecx.images-amazon.com/images/I/71kIifYTccL._SL1500_.jpg
I'm using another custom plugin to build out the content for posts, and so far am just using this shortcode as part of the post content. I would like to do some other things, such as set the featured image, etc. and need that URL.
I've tried
echo do_shortcode( '[amazon template=image&asin=B00FYY53B8]' );
But it doesn't return anything. Is there a way to "execute" shortcodes in a plugin and save the output?
Ultimately, I would also like to scale this functionality so I can replace other/old shortcodes with their HTML output, and save it back to the post, essentially eliminating the use of some shortcodes in posts.
I have a demo for add a short code in wordpress. I hope it help.
add_action('plugins_loaded','MOD_load');
function MOD_load() {
short_enable_form();
}
function short_enable_form(){
if(!function_exists('add_shortcode')) {
return;
}
add_shortcode('form_mod' , array(&$this, 'out_put_form'));
}
public function out_put_form(){
return 'HTML TEXT';
}
You can include this code in functions.php. And call this function in content-singular.php or the similar file in your theme.
function updatesc_database( $post ) {
if ( empty($post) ) global $post;
if ( empty($post) || ! isset($post->post_content) ) return false;
$content = $post->post_content;
$shortc1="amazon template=image&asin";
$shortc2="B00FYY53B8]"; //insert the number variable here
$shortwh=$shortc1.$shortc2;
$replace = do_shortcode($shortwh);
$content = str_replace( $shortwh, $replace, $content );
return $content;
}
I am just working on filter titles in pages and posts and everything works as expected but when I view the single.php whereas it shows the actual post as should be, the function hook filter i am working on inside functions.php of my plugin is filtering every title of each post of my blog, I was expecting to filter just the title of the current post:
add_filter( 'the_title', 'ta_modified_post_title');
function ta_modified_post_title ($title) {
if((in_the_loop())){
/**/
}
}
any help is grateful.
Thanks ;)
add_filter( 'the_title', 'ta_modified_post_title');
function ta_modified_post_title ($title) {
if((some condition true)){
$add_this = 'some string';
$new_title = $title.' '.$add_this;
return $new_title;
}
return $title;
}
when you view any post it is shown through single.php so every post becomes current post when you view it
I understand now that for the filter title hook it is the same viewing a single post from single.php than viewing the whole blog. would there be a way of filtering with a condition to only retrieve the title of the current post in single.php?, I did try "get_the_title(get_the_ID())" like this but obviously I cant nest it.
add_filter( 'the_title', 'ta_modified_post_title');
function ta_modified_post_title ($title) {
if((in_the_loop())){
if(is_single()){
$title=get_the_title(get_the_ID());
}
}
}
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.