I would like to use a php variable to input the value as the shortcode name for wordpress.
Basically I have an option where people can create an options and give it a shortcode name.
Something like the following:
Is this possible?
$shortcode_name = "name_here";
function $shortcode_name ($atts) {
// code here
}
add_shortcode ($shortcode_name , $shortcode_name);
Thanks
In PHP 5.3+ you have closures.
$shortcode_name = 'awesome';
${"{$shortcode_name}_fn"} = function($atts) {
print('Awesome shortcode');
};
add_shortcode($shortcode_name, ${"{$shortcode_name}_fn"});
You can also have variable variable names, in this case, the variable that contains the function is named: awesome_fn.
Related
I need to pass a variable (string or number) to an HTML element generated with WP Bakery Page Builder. I tried with shortcode, but I can't pass a variable but online an "hard coded" string.
I tried this:
global $my_var;
$my_var = "1234";
function wpc_vc_shortcode($my_var) {
return $my_var; // (I tried also with: echo $my_var;)
}
add_shortcode( 'my_vc_php_output', 'wpc_vc_shortcode');
Is there method to use wp all export php functions to replace data or maybe any other statement we can use directly inside custom XML export.
What i need is to achieve this logic
I have custom taxonomy in XML called {Conditions} which return value of each post as "Used" "New"
Values appear correctly, but where i need to export XML required different values then those above, which i need to replace during export.
Logic:
If {Conditions} = "Used"
Set Used = "20";
Elseif{Conditions} = "New"
Set New = "10";
WP ALL EXPORT PLUGIN
Please take a look at "Pass Data Through PHP Functions" doc:
"...you can click on the export field and then use the "Export the value returned by a PHP function" option."
https://www.wpallimport.com/documentation/export/pass-data-through-php-functions/
You would specify the function name into the text field as in the screenshot on the documentation page (convert_conditions) or, if running a custom XML export, call the function from the field with something like this:
[convert_conditions({Conditions})]
...and provide the function in the function editor for the field:
function convert_conditions($cond_from_xml = null) {
if($cond_from_xml === 'Used') {
return '20';
} elseif($cond_from_xml === 'New') {
return '10';
} else {
return '0';
}
}
I want to access and use a variable passed in the URL within a WordPress page (not a template file, but the page itself).
Here's what I'm trying to accomplish. I want to create a single page that will display different WP content by using text passed in the URL in shortcodes and elsewhere on the page. So, if the URL was specified as:
www.greenneedham.org/blog/topicpage/?topic=solar
I will be able to use the passed text ('solar') in shortcodes and other places in the page, like this:
[documents category_name="solar" numberposts="-1" orderby="title" order="ASC"]
Ideally, I'd like to be able to create a variable or field that I can simply use within the page text. I just don't know how to accomplish that.
Here's what I've considered/tried:
1) There are plugins that access the passed variables (e.g. URL params), but that would seem to require nested shortcodes:
[documents category_name="[urlparam param="topic" default="home-energy-savings"/]" numberposts="-1" orderby="title" order="ASC"]
That doesn't work, and I'm not aware of any way to accomplish that.
Added: The Shortcode API documentation at http://codex.wordpress.org/Shortcode_API#Square_Brackets explains that the shortcode parser cannot handle square brackets within an attribute. Unfortunately, that's exactly what I'm trying to do.
If that won't work, The question is now - Is there some way within the page content of presenting a shortcode string that has been modified based on a query variable?
EndAdded
2) Using a filter to add the variable allows me to access the variable within a page template (e.g. page.php), but I want to use the value within the page itself.
3) Plugins to allow PHP snippets within a page or post. This seems to be generally discouraged. It's also complex, and the page itself (which may be used by non-coders) would get hard to use.
Any help or guidance would be much appreciated. Thanks!
add_shortcode('cat', 'get_name');
function get_name() {
return '[category_name='.$_GET['topic'].' numberposts="-1" orderby="title" order="ASC"]';
}
or echo do_shortcode('[category_name='.$_GET['topic'].' numberposts="-1" orderby="title" order="ASC"]'); // instead of return.
Should work as snippet function. then use it as [cat] shortcode
In addition, remember to sanitize your $_GET variable. Here's an example in the context of a simple shortcode to display a query variable within the page text, with reference to where I took the sanitizing code from:
//[query_string label="" class="" var=""] shortcode
function query_string_shortcode( $atts, $content = null ) {
//Create an array of default values for attributes. These don't have to be blank. If specified as attributes in the shortcode, those values will be used instead.
$a = shortcode_atts( array(
'label' => '',
'class' => '',
'var' => '',
), $atts );
//Create variables from atts/defaults
$label = $a['label'];
$class = $a['class'];
/*
* 1. Check if the variable is set
* 2. If so, sanitize the input for security (https://wordpress.stackexchange.com/questions/351292/sanitize-get-query-var-url-parameters)
* 3. Set the variable
*/
$var = ( isset( $_GET[$a['var']] ) ) ? sanitize_text_field( $_GET[$a['var']] ) : '';
if(!empty($label)) {
$label = "<b>$label</b>";
}
if(!empty($var)) {
$html = '<span class="pt-query ' . $class . '">' . $label . $var . '</span>';
return $html;
}
return;
}
add_shortcode( 'query_string', 'query_string_shortcode' );
I have a wordpress functions file in the themes directory running some code in order to apply custom fields to the Job Manager plugin.
The code runs fine, and everything works in order for example it allows you to enter the data and the data will then be displayed on the front end, however not where I want it to display.
I have tried including this functions file, into another php file where I am designing the look on the front end. However, when including the file the variables are not accessible and even though I am echoing the variable name out, nothing appears?
I have tried using a SESSION but even this doesn't work?
function display_job_salary_data() {
global $post;
$salary = get_post_meta( $post->ID, '_job_salary', true );
$salary = number_format($salary);
if ( $salary ) {
echo esc_html($salary);
}
}
Salary is the variable that I need to access in another php file
When a variable is created inside in a function, it's only accessible within the scope of that function.
If you want to use the value in any other way, the function needs to return the value.
Since your function name starts with display_, it wouldn't be very intuitive if it returned something, so I would break it up into two different functions.
First function gets and returns the salary data:
function get_job_salary_data() {
global $post;
$salary = get_post_meta( $post->ID, '_job_salary', true );
$salary = number_format($salary);
return $salary;
}
The second function outputs the salary data: (Just like it did before)
function display_job_salary_data() {
// Get the salary data from our previous function
$salary = get_job_salary_data();
if ( $salary ) {
echo esc_html($salary);
}
}
Now you can get the salary data where ever (as long as the file with the functions are included):
// Get the salary data
$salary = get_job_salary_data();
// ...or just output it, just like before
display_job_salary_data();
To read more about variable scopes, I recommend reading PHP's documentation about the subject here: http://php.net/manual/en/language.variables.scope.php
I want to develop a plugin to add the ability to do something with shortcode. I want it to function like this:
[shortcode]Content[/shortcode]
Here is the code I use:
function quote( $atts, $content = null ) {
return '<div class="right text">"'.$content.'"</div>';
}
add_shortcode("quote", "quote");
The $content variable, which returns the value of the shortcode, in this case Content, cannot be used outside of the function. I want to use it on some other part of the PHP code, but I can't get it to work. I am not experienced with PHP, so if you have any solution, please try to be as clear as possible.
Thanks.
You'd have to declare it as a global variable otherwise it's scope (where you can access it) is limited to the function you're using it in.
function quote( $atts, $content = null ) {
global $content;
return '<div class="right text">"'.$content.'"</div>';
}
add_shortcode("quote", "quote");
echo "Using content somewhere else $content";
FYI though, it can lead to potential problems. $content, for example, is a pretty common variable and could conflict if the same variable is being used elsewhere. You'd be better off giving it a unique name like: global $my_global_content = $content. Then use $my_global_$content in the other areas of your code.