shell_exec / require_once / Nothing is working in plugin add_action - wordpress

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.

Related

rename() not working Wordpress plugin

I have a Wordpress plugin that will take the name of one directory on the server and rename to a name set in a Settings/Options field. The code I'm using is:
$base = basename(dirname(__FILE__));
$path = (isset($xgeneral['obf_plugin']))
?$xgeneral['obf_plugin']
:'original_directory';
if ($path != $base) {
$former = $base . DIRECTORY_SEPARATOR . 'mig_plugin.php';
$newer = $path . DIRECTORY_SEPARATOR . 'mig_plugin.php';
rename($former,$newer);
}
So what happens is when the plugin loads the first time, it checks if the obf_plugin option is the same as the directory in question; if they are different, it then renames it.
However, no matter what I try I get the error rename(migratex/mig_plugin.php,tango/mig_plugin.php): no such file or directory (tango is just the dumb sample word I tried to use) I tried referencing the absolute file path on the server using this code:
define("DS","/",true);
define('BASE_PATH',realpath(dirname(__FILE__)).DS,true);
but that doesn't make a difference.
Is there any reason why the rename() function is not working? It is running inside the Wordpress Admin page.
Thank you!
EDIT: Further investigation reveals this works in plain PHP but will not when running in Wordpress. Any thoughts as to how to enable rename() in Wordpress?

Getting path to Drupal root while in module

I am not sure if this is an issue with my current setup, or what I want to do.
I have a module that is programatically creating nodes in my Drupal 6 site, and within each I have to provide links in between various nodes.
I basically have a few foreach loops, and within each I have the current path.
For instance:
foreach ($page->category as $category) {
$category_link = "category/" . $category['id'];
// generate category pages
...
$content = "<a href='$category_link'>".$category['name']."</a>";
_create_node($content);
foreach ($category->article as $article) {
$article_link = $category_link . "/article/" . $article['id'];
// generate article page
$content = "<a href='$category_link'>".$category['name']."</a>";
$content .= "<a href='$article_link'>".$article['name']."</a>";
_create_node($content);
}
}
The issue that I'm seeing is that the link seems to be continually built up.
For instance, in the main category pages it is fine (I'll see category/1234), and the article link will be fine, but the category link will seem to be longer than it should. Basically, I'll end up seeing:
category/1234/article/5678/category/1234
My first thought was to make use of $base_url and just create absolute paths, however whenever I try printing that variable from my module it is completely empty. This is on a local server, however when I move it to production Drupal isn't installed at the root, so I can't simply add a slash to the front of the link.
Try using $GLOBALS['base_path'] to get the base path.
$GLOBALS['base_path'] will work, but you are accessing a global variable that ALSO contains some things like your database connection info and some other important stuff. So with a slip of the finger you could muck up other things. I prefer base_path() which does the same thing but is a modicum safer.
Use
global $base_url;
For path to themes folder use
path_to_theme()
You can use base_path() but that will not provide you with the domain name.
Base url will provide you the complete url like : www.example.com
base_path() will give you : /
path_to_theme() will give you : sites/all/themes/yourthemename

wordpress wp_enqueue_script is not working in actions but working normally .

add_action("publish_post", "php_func");
function php_func(){
wp_register_script("customscript","link to js file");
wp_enqueue_script("customerscript")
}
I think above process is right, but thats not working . The two lines in above func are working normally when written in the php file directly(without actions), but with actions, above is not working .
function php_func(){
echo "<script>alert("hiii");</script>"; //working but not good method. Also, getting errors like headers sent already .
}
Every action is registered to a specific hook. When that hook is reached in the code, all actions registered to that hook is run.
When you publish a post the publish_post hook will run you function, and enqueue you javascript.
Your problem is that the code, then loops through all enqueued scripts and run them, has already been run.
You need to enqueue them in an earlier hook. And the specific hook build for this is called wp_enqueue_scripts.
add_action("wp_enqueue_scripts", "php_func");
function php_func(){
wp_register_script("customscript","link to js file");
wp_enqueue_script("customerscript")
}
There is a lot more information to be found in the Codex.

WordPress and Global Variables

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.

Grabbing Get option function from php file in plugin folders

Really need some help i am making a plugin within the main plugin i have the code.
// Some Defaults
$bucket = 'music';
// Put our defaults in the "wp-options" table
add_option("isd-bucket", $bucket);
so thats great for me i no it adds to the database music.
the problem i am have is i need to pull that out off the database into another php file in my plugin folder.
in my playlist.php i have.
echo get_option("isd-bucket");
problem is when running this file i get.
Call to undefined function get_option() in
I understand this probably means i need to require other files so it intergrates with wordpress i have tried many options but cant get it to work plese help.
Thanks
Be sure to remove the extra white space (blank line) between the lines of code.
// Some Defaults
$bucket = 'music';
// Put our defaults in the "wp-options" table
add_option( "isd-bucket" , $bucket );

Resources