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
Related
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'm developing a Wordpress theme and I'm trying to store some of my variables in a function to re-use it among my theme files, without writing the same variables hundreds of time and without make them global. I've read that's a bad practice.
For now I'm using add_action but without results. Having in my functions.php
add_action( 'run_pms_variables', 'pms_variables_function' );
function pms_variables_function ($uid ) {
$the_group_uid = isset($_COOKIE["v_group_id"]) && !empty($_COOKIE["v_group_id"]) ? trim(strip_tags($_COOKIE["v_group_id"])) : "";
$session_detail = vpb_get_the_user_detail($uid);
$session_username = strlen($session_detail[0]) > 40 ? ucfirst(substr($session_detail[0],0,40)) : ucfirst($session_detail[0]);
$session_uid = $session_detail[1];
$session_email = $session_detail[2];
$session_photo = $session_detail[3];
$session_country = $session_detail[4];
//$session_usernames = explode(' ', $session_detail[0]);
$session_firstname = get_user_meta($uid,'first_name', true );
$session_lastname = get_user_meta($uid,'last_name', true );
}
and using eventually in my files like:
do_action('run_pms_variables', $uid );
I mean, can you address me to the right method? Thanks.
Write a class, declare a global object of that class, save all your variables as attribute/property of that class.
Now from anywhere just get the global object.
You have all variables stored inside that.
Uuuuhmm you are mixing apples with peaches. The WordPress Hooks, Actions and Filters are ment to modify the WordPress core the way you want, it is not build to store any data somewhere in your template.
One way to do it would be to simply store the values in the database of your WordPress installation. To do that you need to get familiar with the "Nonce" function, which is security mechanism for storing stuff in the database of WP.
There are hundrets and thousands of tutorials out there which show how to do it.
If you want only to store some numeric values or short strings, you could use the HTML data-* object to store some data, when php spits it back.
add_action( 'run_pms_variables', 'pms_variables_function' );
function pms_variables_function ($uid ) {
class GlobalVariables{
function __construct ($uid){
$this->the_group_uid = isset($_COOKIE["v_group_id"]) && !empty($_COOKIE["v_group_id"]) ? trim(strip_tags($_COOKIE["v_group_id"])) : "";
$this->session_detail = vpb_get_the_user_detail($uid);
$this->session_username = strlen($session_detail[0]) > 40 ? ucfirst(substr($session_detail[0],0,40)) : ucfirst($session_detail[0]);
$this->session_uid = $session_detail[1];
$this->session_email = $session_detail[2];
$this->session_photo = $session_detail[3];
$this->session_country = $session_detail[4];
//$session_usernames = explode(' ', $session_detail[0]);
$this->session_firstname = get_user_meta($uid,'first_name', true );
$this->session_lastname = get_user_meta($uid,'last_name', true );
}
}
return GlobalVariables($uid);
}
//and to access:
$global_variables = do_action('run_pms_variables', $uid );
echo $global_variables->the_group_uid;
As a side note, be definition global variables are variables that can be accessed anywhere, other options include setting variables in the $_SESSION super global (as most of you're variables look session specific) inside the pms_variables_function instead of setting class attributes.
Hello community WordPress forum.
I have a need to write the posts the output of a function, but all to getting to do is to display real-time and run this script / function in all posts, with I do to write the posts the output of a function instead of display it in posts?
because what 's happening is that the script is running on all posts, and each refresh / access the pages, a new script number is generated! I would like the generator create a different number for each post, but write to output them, and not display a number to each new access.
// Declare the function
function gen_num()
{
// DETERMINE THE CHARACTER THAT CONTAIN THE PASSWORD
$caracteres = "012345678910111213141516171819";
// Shuffles THE CHARACTER AND HANDLE ONLY THE FIRST 10
$mistura = substr(str_shuffle($caracteres),0,10);
// DISPLAYS THE OUTCOME
print $mistura;
}
// Add custom post content, inject function in content.
function add_post_content($content) {
gen_num();
return $content;
}
add_filter('the_content', 'add_post_content');
see in herculestest.tk, browse the pages, make f5 to refresh.
Thank you very much.
==========================================
another attempt:
I created a custom field from ACF plugin named: numeration:
function gen_num()
{
global $post;
$mistura = get_post_meta( $post->ID, 'numeration', true );
if ( '' == $mistura ) {
//DETERMINE THE CHARACTER THAT CONTAIN THE PASSWORD
$caracteres = "012345678910111213141516171819";
// Shuffles THE CHARACTER AND HANDLE ONLY THE FIRST 10
$mistura = substr(str_shuffle($caracteres),0,10);
update_post_meta( $post->ID, 'numeration', $mistura );
}
//DISPLAYS THE OUTCOME
print $mistura;
}
// Add custom post content, inject function in content.
function add_post_content($content) {
gen_num();
return $content;
}
add_filter('the_content', 'add_post_content');
this solution to not change the number each new access, now writing these data permanently in the database ?? because're not recording! if I change my theme, all the numbers in all the posts disappear, and if I make any errors in functions, php, these add up numbers, because they depend on the function running at him display the values, and worst of all, to fix the functions.php, the script will return to run and therefore, will be a re-run, which means that it will generate new numbers on all posts !! and this can not happen, should I ever have the same values!
The Post ID is unique in WordPress, is that number adequate for your needs?
Otherwise you'd need to generate a unique ID when the post is published and save it in a custom field. (Instead of your gener_numb function look into uniqid: http://php.net/manual/en/function.uniqid.php.)
Can anyone advice how best to set a temporary variable with scope to be used between hooks?
I have a custom module with the purpose of checking and warning if an attempted file upload matches an existing file on the system.
I am using hook_form_alter to identify the specific upload form and hook_file_validate to check if the file was previously uploaded.
My problem is those two hooks don't share a common parameter - I need to pass information from hook_form_alter back to hook_validate.
The information I want the functions to share is a simple boolean which should be destroyed immediately the file upload is done/dismissed, so using variable_set to persist it to the database is overkill. I don't believe a session or cookie approach is best either.
UPDATES:
Globals approach:
function duplicate_uploads_warning_init(){
$GLOBALS['processed'] = 'testing';
}
function duplicate_uploads_warning_file_validate($file){
drupal_set_message("PROCESSED[file-validate]: {$GLOBALS['processed']}", 'warning');
}
function duplicate_uploads_warning_form_alter(&$form, &$form_state, $form_id){
if( $form_id == 'file_entity_add_upload' ){
drupal_set_message("PROCESSED[form-alter]: {$GLOBALS['processed']}", 'error');
$GLOBALS['processed'] = 'tested';
$form['#validate'][] = 'duplication_validate';
}
}
The code above sets GLOBALS[processed] in the init hook and that value in immediately confirmed in the hook_form_alter.
However the attempt to reassign the value to tested fails. The reassigned value is what I hoped to see in hook_file_validate but I still get the initial value of testing.
Hook_form_alter validation approach:
I tried adding a custom validation function but the upload of the image still took place where I intend to stop it. My code is as follows:
function duplication_validate($form, &$form_state) {
$data = duplicates($form_state['complete form']['upload']['#file']->filename);
if( sizeof($data) > 0 ){
form_set_error('test', 'testing validation');
return false;
}
}
I can confirm my $data variable has content and the sizeof test returns greater than 0.
using variable_set to persist it to the database is overkill
I don't agree they are overkill (you can easily delete variables), and as #2pha have mentioned globals are not recommended. I think you could use variable_set($name, $value), variable_get($name), and variable_del($name) without worrying unless you need to super-optimise your sites database queries. If you really don't want to use the variable_* functions, then maybe cache_set() and cache_get() might work because you can give it a temporary status.
Edit, variables approach:
function duplicate_uploads_warning_init(){
$account = \Drupal::currentUser();
variable_set($account->id() . '_processed', 'testing');
}
function duplicate_uploads_warning_file_validate($file){
$account = \Drupal::currentUser();
$processed_var = variable_get($account->id() . '_processed');
drupal_set_message("PROCESSED[file-validate]: {$processed_var}", 'warning');
}
function duplicate_uploads_warning_form_alter(&$form, &$form_state, $form_id){
if( $form_id == 'file_entity_add_upload' ){
$account = \Drupal::currentUser();
$processed_var = variable_get($account->id() . '_processed');
drupal_set_message("PROCESSED[form-alter]: {$processed_var}", 'error');
variable_set($account->id() . '_processed', 'tested');
$form['#validate'][] = 'duplication_validate';
}
}
… and in a success or #submit callback function run variable_del($account->id() . '_processed') to delete the variable.
maybe $GLOBALS['processed'] was initialized with the static keyword elsewhere....?
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.