WordPress and Global Variables - wordpress

I have a little piece of code inside my function.php file, and I can't access a global variable. I copy this in a separate php file and I get 'New value' but not inside the theme's function.php file:
$myVar = 'test';
function hello() {
global $myVar;
$myVar = 'New value';
}
hello();
echo $myVar;
and it prints out 'test';
Does WP has problems with globals? As far as I know, WP backend extensively uses global vars.

In a simple PHP file this works for me -- i.e. I get "New value". Something must be missing from what you presented as your execution context.
As for WP having a problem with globals, I think the more general statement is that PHP programs have a problem with globals in that they use/depend on waaaay too many of them. Unfortunately, it seems to be the nature of the beast.

Related

shell_exec / require_once / Nothing is working in plugin add_action

Worpress Site. I'm exporting order information from WooCommerce. I can run shell_exec from the functions.php file but whenever it's run inside of an action (add_action) it doesn't execute. I've tried require_once as well as a number of other options. I can make them run from command line, just not from within a plugin hook. Thank you in advance.
Pretty much everything. I've tested all the options within command line using stand alone php scripts and it works fine.
add_action( 'woocommerce_order_status_changed', 'live_order_info');
function live_order_info(){
$vars = "ANYDATA";
$command = escapeshellcmd("./test.php ");
$output = shell_exec($command.$vars);
echo $output; }
I can verify that the code is doing something, just not causing the test.php script to execute. All files have permissions set and work correctly if called from putty.
The fix was the file path. I had to use the absolute file path and it worked perfectly.
Putty was using the ~ symbol where it wasn't noticed and left out of the function. I added it the file path and presto.

What is different between post_navigation() and get_the_post_navigation();?

What is difference between these WordPress functions, and how to implement it?
the_post_navigation(); get_the_post_navigation();
the_archive_title(); get_the_archive_title();
the_post_pagination(); get_the_post_navigation();
the_archive_description(); get_the_archive_description();
I already googled for this, but I'm still not getting it right.
All the functions that starts with get_ are only returning the "result" of the function : If you put this function in a php page and watch this page in a browser, nothing will be displayed.
If you want the result to be displayed, you have to add echo before the function and this is exactly what the function that starts with the_ are doing.
You may ask yourself why sometimes we want only the result to be returned by the function and not displayed. It's because sometime, we have to do some additional operations with the result before displaying it.
Example:
$content = get_the_content();
$content = str_replace('Hello', 'Bye', $content);
echo $content;
If not operation are needed so you only need to do:
the_content();
You also ask "How to implemenent it ?". To implement the function, you will have to add it in some specific php files. For example, for the get_the_post_navigation() function you will have to add it in the single.php file in your theme folder. You will need basics on php.

How to re-use Drupal module code using module_load_include

I am using the node_clone module with great effect but there is a need in my project to use node_clone functions from my custom module. So I put in the following code:
module_load_include('inc', 'node_clone', 'clone.pages');
function mymodule_init(){
clone_node_save(118);
}
That code returns Fatal error: Call to undefined function clone_node_save().
My modules are categorized by source into directories labelled mine and contrib. Node_save is in contrib while myModule is in mine.
So, I amended the code acordingly as follows:
module_load_include('inc', '../../contrib/node_clone', 'clone.pages');
but I get the same error.
What am I doing wrong?
Use:
require_once DRUPAL_ROOT . '/sites/all/modules/contrib/node_clone/clone.pages.inc';
From the module_load_include API:
Do not use this function in a global context since it requires Drupal
to be fully bootstrapped, use require_once DRUPAL_ROOT .
'/path/file' instead.
It's a bit misleading, the folder is named 'node_clone' but the module is actually called 'clone', so you want:
module_load_include('inc', 'clone', 'clone.pages');
hook_init() runs pretty early on so if you don't need the clone module's functions before hand, you'd be better off moving the code into the hook:
function mymodule_init(){
module_load_include('inc', 'clone', 'clone.pages');
clone_node_save(118);
}

Wordpress - Change page dynamically but within shortcode running php

I am using a custom shortcode plugin. The plugin allows me to run some php. The php queries a non-wp database to build a page of vehicle specs and everything works great. The issue is that all of the information needed for the page title and description is contained within the data coming from the database. I've tried some of the standard wp php filters but the title does not change.
Is this not possibly because of the execution timing of the shortcode?
TinyMCE in WP admin. Shortcode
-----------------------------------------------
[myplugin data_id='42']
PHP window in shortcode editor
-----------------------------------------------
$GP=array_merge($_GET, $_POST);
echo "hello word" //works
echo $data_id; //works
echo $GP[some_post_data]; //works
//connect to database (irrelevant)
echo "the title from data table for data_id 42 = ".$data[title]; //works
// the following has no effect on page title even though $data[title] contains valid data
add_filter('the_title','myCallback');
function myCallback($data){
return $data[title];
}
The problem is that $data[title] is out of scope, and that your filter callback is set up incorrectly. Also, you SHOULD encapsulate associative indexes with quotes.
When adding a callback to an existing filter, the arguments within the callback are passed by the filter definition. Case and point: Filtering The Title
The arguments within a standard filter for the_title are $title and $id. If you want to return the title from your $data array, you can do it the sloppy way using the global scope:
add_filter('the_title','myCallback');
function myCallback($title, $id){
global $data;
return $data['title'];
}
But personally, I would look into avoiding globals altogether and concentrate on utilizing custom filters. Look into Adding Your Own Hooks. After getting a handle on action hooks, Filter Hooks are pretty easy to grasp.

Set PHP Variables for Drupal 7 Theme Files

I want to set custom php variables that can be used throughout my drupal theme (html.tpl.php, page.tpl.php etc.) I need the variables set based on the $title of the node. I'm not an expert on how Drupal works, the moduling and hooks, and I just need to know the simplest/easiest way to accomplish this. I keep reading about implementing some sort of hook in the template.php file where you can set variables, but I've been unsuccesful with everything I've tried.
So, basically, how would you accomplish this:
Get $title of Node
Set variables that will be passed along into theme files (for example, to do basic things like: if($title == 'news_page') { $siteSection = "news"; } )
Have $siteSection be available to use in theme files
Any help would be great.. thanks!
Before Drupal builds the HTML for a page from a theme's template (.tpl.php file), it runs preprocess "hooks". Hooks are basically a naming convention for functions that let modules and themes override or "hook" onto Drupal core processes.
E.g., if you want to display a message to a user when they log in, you can use hook_user_login.
function MODULENAME_user_login(&$edit, $account) {
drupal_set_message("Welcome, ". $account->name);
}
When a user logs in, Drupal looks for all loaded functions that end in "_user_login" and it runs them. If this function is in an enabled module, it has been loaded, so it will get run as well.
If you want to make a variable named $site_section available in your page.tpl.php file, you can hook into template_preprocess_page. This is a theme hook, so the name is a little different, but it functions pretty much the same way. To call this hook from your theme, you need to create a file called template.php in your theme's directory. Inside template.php, we'll add:
<?php
function THEMENAME_preprocess_page(&$vars){
switch (drupal_strtolower($vars['node']->title)) {
case "about page":
$site_section = "about";
break;
case "news page":
case "news page1":
case "news page2":
$site_section = "news";
break;
default:
$site_section = "none";
break;
}
$vars['site_section'] = $site_section;
}
The <?php is used to tell the server the treat all of the proceeding code as PHP. We then declare our hook function with the intention of loading Drupal's array of page variables into a local variable called $vars. By adding the & before $vars, we'll be allowed to modify the values for use outside of this function.
The switch statement will let us efficiently test the page title for multiple values. The value of the node's title may contain uppercase letters, lowercase letters, and symbols, so to avoid a case-sensitive mismatch, we're going to convert the title to lowercase and only test that (symbols will still be in the title, though). After the switch statement, we set the value of our $site_section local value into the referenced $vars array for use in page.tpl.php.
However, if it's just your intention to break the site up into sections for theming purposes, there are other ways of accomplishing that. My answer to a similar situation a few months ago might be helpful.

Resources