I am trying to create a shortcode in Wordpress, in which the function that is called with the shortcode tag gets the shortcode tag as parameter.
So say I have
<?php
var $shortcode = 'my_shortcode_tag';
add_shortcode( $shortcode, 'my_shortcode_function');
?>
then I want 'my_shortcode_function' to know the shortcode tag by it was called. I know that I could use attributes as in [my_shortcode_tag tag='my_shortcode_tag'] when I call the shortcode in my actual post, but I want to be able to just write [my_shortcode_tag] and my function to know that it was called by that tag. Is there a way to do this?
This is sent in as the third argument to a shortcode function (as mentioned in the Shortcodes API).
for example:
add_shortcode( 'shortcode1', 'my_shortcode_function');
add_shortcode( 'shortcode2', 'my_shortcode_function');
function my_shortcode_function($atts, $content, $sc) {
return $sc;
}
this will output the name of the shortcode called for that function.
Related
I am trying to call function, created with add_action using do_action:
In the function.php of the theme:
function bleute_utbi_service_type()
{
return "service";
}
add_action('utbi_service_type', 'bleute_utbi_service_type');
Now, I need to get the value of the function in a plugin file:
// 'x' plugin file:
function get_valor(){
$val = do_action('utbi_service_type');
echo "this is the valor:" . $val";
}
This way of doing is not working, $val return 'null'...why?
Action hooks do not return content, and honestly if you need an action hook to return content there is a pretty good chance that you are doing something wrong.
add_action() takes the identification of a function, in your case
bleute_utbi_service_type(), and puts it into a list of functions to be called
whenever anybody calls do_action().
Either use $params with your do_action and add_action and then set the $value in your add_action callback function or use filters to return contents. To know how return works with filters, u may refer here: Wordpress: How to return value when use add_filter? or https://developer.wordpress.org/reference/functions/add_filter/
If you want to do it with add_action than you have to follow this referenece by passing argument to add_action
Reference
else try using apply filters like this.
add above code in function.php:
function example_callback( $string ) {
return $string;
}
add_filter( 'example_filter', 'example_callback', 10, 1 );
function get_valor(){
$val = apply_filters( 'example_filter', 'filter me' );
echo "this is the valor:". $val;
}
and add below code to wherever you want to print:
get_valor();
hope it works for you :)
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'm writing a plugin and have code like this:
add_shortcode('test','testfunc');
function testfunc(){
add_filter('the_content','mycontent',20);
}
function mycontent($content){
return "<p style='color:red'>extra content</p>";
}
I have used the shortcode '[test]' on one of my posts.
The problem is when a list of posts is shown - for example when using a category view - the content is changed for all of the posts displayed - not just the one with the shortcode in it.
Any ideas how I can change it so it only filters the code
Hi heres a solution is doesn't use a shortcode hook but it searches for it in the content.
function content_custom_shortcode($content) {
// Search for shortcode
$shortcode = 'test';
preg_match('/\['.$shortcode.'\]/s', $content, $matches);
// If custom shortcode exists return custom content else return content
if (in_array('['.$shortcode.']', $matches)) return "<p style='color:red'>extra content</p>";
else return $content;
}
add_filter('the_content','content_custom_shortcode',20);
Any time you display a list of posts and one of them contains the shortcode you've created it will change all following posts that are displayed because you've added it to the_content, which is executed on every iteration of The Loop. Remove the filter and it should work as expected.
This will wrap the content that you surround with [test] and [/test] in the <p> tags:
add_shortcode('test','testfunc');
function testfunc($atts, $content=NULL){
return "<p style='color:red'>{$content}</p>";
}
If you would like to replace all of the content in the post with replacement text change the variable {$content} in the example above to whatever text you would like. You still need to wrap the post content in [test] and [/test].
There is an example in the WordPress Codex entry on shortcodes that is very similar to this.
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'm trying to add a code to the_content() function. I tried the following
function add_something($content) {
echo "test";
}
add_filter( 'the_content', 'add_something', 6);
After adding the filter my posts return just the echo test without the content. How can I execute my custom code without omitting the content?
I would surmise you need something like that (if it's indeed a filter callback):
function add_something($content) {
return "test" . $content;
}
Seems what the docs say:
http://wordpress.org/support/topic/add-something-to-the_content
you omitted the return statement.
function add_something($content) {
echo "test";
... change $content ......
return $content;
}
Be advised that if you want to modify the content, you must append it to the variable. Using echo will output 'test' at the time the script is called. It will not append or prepend it to the_content()
Use this (the_content filter needs 1 parameter)
add_filter( 'the_content', 'add_something', 1);