Get variable from a page to index page in wordpress - php

I have a variable called $total in a page called page-pants.php,
i need to echo the variable called $total in index.php page.
how it can be done?

Best thing to use is a non expiring transient. Set your $total with the following code in your page-pants.php file. I am assuming that the $total value is changing each time you visit the pants page.
delete_transient( 'pants_total' );
// Save $total as the transient pants_total
set_transient( 'pants_total', $total );
Then you can access the $total anywhere in your WordPress site with the following
$total = get_transient( 'pants_total' );
echo $total;

You can define and set that in your functions.php and use that in everywhere that you need.
For example:
add_action('init', 'myStartSession', 1);
add_action('wp_logout', 'myEndSession');
add_action('wp_login', 'myEndSession');
function myStartSession() {
if(!session_id()) {
session_start();
}
$_SESSION['myKey'] = "Some data I need later";
}
function myEndSession() {
session_destroy ();
}
And show that in each theme file that you need:
if(isset($_SESSION['myKey'])) {
$value = $_SESSION['myKey'];
} else {
$value = '';
}
echo $value;

Related

How to extract variable value from inside a wordpress plugin code that's inside wordpress theme functions file

I added the following plugin hook inside my WordPress child theme specifically inside the functions.php file.
add_action('frm_display_form_action', 'check_entry_count', 8, 3);
function check_entry_count($params, $fields, $form){
global $user_ID;
remove_filter('frm_continue_to_new', '__return_false', 50);
if($form->id == 11 and !is_admin()){ //replace 5 with the ID of your form
global $count;
$count = FrmEntry::getRecordCount("form_id=". $form->id ." AND user_id=".$user_ID);
if($count >= 1){ //change 2 to your entry limit
echo 'This form is closed';
add_filter('frm_continue_to_new', '__return_false', 50);
}}}
I need to get the value of $count outside the plugin hook code above. Outside the hook, the variable $count is null despite assigning it a global scope.
I also tried to run create a test function - different from $count but it did not work.
Example:
add_action('frm_display_form_action', 'check_entry_count', 8, 3);
function check_entry_count($params, $fields, $form){
//Testing a different global variable
global $ww_new;
$ww_new = 'testing';
global $user_ID;
remove_filter('frm_continue_to_new', '__return_false', 50);
if($form->id == 11 and !is_admin()){ //replace 5 with the ID of your form
global $count;
$count = FrmEntry::getRecordCount("form_id=". $form->id ." AND user_id=".$user_ID);
if($count >= 1){ //change 2 to your entry limit
echo 'This form is closed';
add_filter('frm_continue_to_new', '__return_false', 50);
}}}
//Echoed the test using the test global values above
function ww_new_usage() {
global $ww_new;
echo '<script type="text/javascript">alert("'.$ww_new.'");</script>';
}
//$ww_new also returns Null.
How can I get the value of the global variable $count?
Is it a priority issue? If yes, how can I go about solving it? Anything missing?

How to declare and use global variables

On this test page https://wintoweb.com/sandbox/question_2.php , the visitor can make searches in the DB and tick as many checkboxes as wished. When button [Accept...] is clicked, I want the result of all searches to be shown under 'Your selections so far'. Right now, only the last search is displayed. I tried using a global array to store the result of previous searches and increment it upon each new one. That's where I have a problem.
On top of file I have :
<?php
global $all_authors;
array ($all_authors, '');
?>
At bottom of file I have :
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
if(isset($_GET['search'])){
//echo 'Search</br>';
} elseif(isset($_GET['display_this'])) {
echo getNames();
}
function getNames() {
$rets = '';
if(isset($_GET['choices']) and !empty($_GET['choices'])){
foreach($_GET['choices'] as $selected){
$rets .= $selected.' -- ';
}
//array_push($all_authors, $rets); // This is the problem
//print_r($allAuthors); // this too
echo '</br><b>Your selections so far :</b></br>';
}
return $rets;
}
?>
EXPECTED: Results of all previous searches to be listed
ACTUAL: No go due to problem with array_push(). See function gatNames()
You should make the array global inside the function, so on top:
$all_authors = array();
In the bottom:
function getNames() {
global $all_authors;
// Do the rest of the stuff
}
You are returning $rets from your getNames function but not using it. You just need to use this variables $rets instead of Global Variable.
if(isset($_GET['search'])){
//echo 'Search</br>';
} elseif(isset($_GET['display_this'])) {
$rets = getNames(); //The $rets will hold the value returned by your function getName().
if( !empty ( $rets ) ) {
echo '</br><b>Your selections so far :</b></br>';
echo $rets;
}
}
You could remove the echo statement from inside your getNames Method.
function getNames() {
$rets = '';
if(isset($_GET['choices']) and !empty($_GET['choices'])){
foreach($_GET['choices'] as $selected){
$rets .= $selected.' -- ';
}
}
return $rets;
}

include a php page based upon the value of a variable

I have made a function that take in the current page id and based on that result will either show two .php file or just one .php file.
Following is what I have written. Have I approached this in the right way?
<?php
function get_search_or_nav($page_id) {
if(isset($page_id)) {
$id = $page_id;
$pages = array('home', 'thank-you');
foreach($pages as $page){
if($page==$id)
$match = true;
}
if($match) {
include("dir/file_1.php");
include("dir/file_2.php");
}
elseif (!$match) {
include("dir/file_1.php");
}
}
}
?>
The $pages variable holds the $page_id array i.e.$pages = array('home', 'thank-you');
each .php file has a $page_id i.e index.php has $page_id = "home";
The array is a list of the matching $page_id's:
$pages = array('home', 'thank-you');
The call would then be:
get_search_or_nav($page_id);
Any help or advise would be appreciated.
I just will do this:
$id = $page_id;
$pages = array('home', 'thank-you');
$match = in_array($id, $pages);
The iteration is not necessary
There is no need for the foreach loop. PHP has inbuilt functions to handle what you want (in_array()): I'd change your function to something like this:
function get_search_or_nav($page_id) {
if (isset($page_id)) {
$id = $page_id;
$pages = array('home', 'thank-you');
// make sure file is in allowed array (meaning it exists)
if(in_array(id, $pages)) {
include("dir/file_1.php");
include("dir/file_2.php");
return true;
}
// no match, so include the other file
include("dir/file_1.php");
return false;
}
}
You could approach your function in following way:
function get_search_or_nav($page_id) {
if(isset($page_id)) {
$pages = array('home', 'thank-you');
// You could do this with for loop
// but PHP offers in_array function that checks
// if the given value exists in an array or not
// if found returns true else false
$match = in_array($page_id, $pages);
// you are including this in either case
// so no need to repeat it twice in both conditions
include("dir/file_1.php");
// if match include file 2
if ($match) {
include("dir/file_2.php");
}
}
// you might want to throw Exception or log error here
// or just redirect to some 404 page or whatever is your requirement
}
in_array reference

how to make session_start change on each refresh

Im currently using the below code on my wordpress to display posts randomly but not duplicate them. This works apart from when I refresh the page it stays the same as it is. The only way to change what is displayed is to clear my cache and cookie. Is it possible to make it so that on each refresh it changes?
Here is my code:
session_start();
add_filter('posts_orderby', 'edit_posts_orderby');
function edit_posts_orderby($orderby_statement) {
$seed = $_SESSION['seed'];
if (empty($seed)) {
$seed = rand();
$_SESSION['seed'] = $seed;
}
$orderby_statement = 'RAND('.$seed.')';
return $orderby_statement;
}
Remove the if condition, in current situation - rand option set only if the session is empty. So that it working at first time i.e session not set.
$seed = rand();
$_SESSION['seed'] = $seed;
instead of
if (empty($seed)) {
$seed = rand();
$_SESSION['seed'] = $seed;
}
you can try out like
session_start();
session_regenerate_id();
echo session_id();
This returns you with unique session ids on each refresh

Where in Wordpress will i include my file so i can access its variables throughout the rest of the page?

I tried including my file in header.php or my theme's index.php, but I cannot access the variables in the included file from let's say my theme's footer.php or even my theme's page templates.
Here's what I'm including:
<?php
// some parameters
$var_research = 5;
$var_researchtrans = 7;
$var_output = 9;
$var_edit_indi = 11;
$var_contact = 15;
$var_transition = 19;
?>
Now what I need is to be able to use the variables in footer.php, for example.
Hope someone out there has an answer. Thanks, y'all.
OK, here's how I made it work:
In functions.php
<?php // functions.php
// ...
function my_var($va_var) {
// some parameters
$var_research = 5;
$var_researchtrans = 7;
$var_output = 9;
$var_edit_indi = 11;
$var_contact = 15;
$var_transition = 19;
$var_sometext = "text test";
eval("\$return_var = $" . $va_var . ";");
return $return_var;
}
?>
and in footer.php
<?php // footer.php
// ...
echo "blah blah " . my_var("var_sometext");
// ...
?>
It's working, but did I do it right? is there a better/right way to do this? Thanks again, everyone.
The best place to include your own functions are in your theme's functions.php file.
If you want to access a variable in multiple files, you can create a function in your functions.php and access that function anywhere within the theme.
In functions.php
function your_variable()
{
$var = 'your variable';
return $var;
}
And in your footer.php
echo your_variable();
For your updated query
function my_var($va_var) {
$out = array();
// some parameters
$out['var_research'] = 5;
$out['var_researchtrans'] = 7;
$out['var_output'] = 9;
$out['var_edit_indi'] = 11;
$out['var_contact'] = 15;
$out['var_transition'] = 19;
$out['var_sometext'] = "text test";
return $out[$va_var];
}
I'm not sure what your function does, but the place to define functions is in your themes functions.php file.
humm , the original solution for this problem is that you must save your vars as an option variable in wordpress , something like this :
<?php add_option( $name, $value, $deprecated, $autoload ); ?>
and the function :
<?php
function my_var($va_var) {
add_option( 'var_research', '5', '', 'yes' );
}
?>
and in the footer.php you just need to use this for retrieve your variable:
<?php _e(get_option('admin_email')); ?>
Good Luck

Categories