How to re-use Drupal module code using module_load_include - drupal

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);
}

Related

Fatal error: Call to undefined function add_action() in

I am trying to add action in a plugin in a file which is included to the base php plugin file:
add_action( 'wp_ajax_my_action', 'my_action' );
Then I am requiring the file in the main plugin file:
require_once plugin_dir_url(__FILE__) . 'api/index.php';
, but I am getting this error:
Fatal error: Call to undefined function add_action() in
This code works fine, if it is in the main plugin file, but if I put it in another file it doesn't.
I have followed some answers to similar questions to include wp-load, but I am getting the same error.
Following this code, I think you might solve your issue.
// You must define the wp_ajax_my_action callback
function my_action( $my_action_callback ) {
// Write your action
};
// add the action
add_action( 'wp_ajax_my_action', 'my_action');
If you want to get more info, please refer to http://hookr.io/actions/wp_ajax_my_action/
Good luck!

Using a Composer class in functions.php

I'm trying to add a shortcode to my Wordpress site to pull a piece of data from a Google spreadsheet and drop it into a page. To do this, I'm trying to use Sheetsu. The php libraries for Sheetsu are managed through Composer.
I've got a working piece of standalone code, but when I drop it in functions.php, like this...
function do_sheetsu() {
require('vendor/autoload.php');
use Sheetsu\Sheetsu;
$sheetsu = new Sheetsu(['sheetId' => '8b297aa81110']);
$response = $sheetsu->search(['id' => '2.05.1']);
$collection = $response->getCollection();
echo $collection->get(0)->answer;
}
add_shortcode('sheetsu','do_sheetsu');
...it blanks my site. If I comment out the use Sheetsu\Sheetsu; line, my site comes back, but I get no output, and the error "PHP Fatal error: Class 'Sheetsu' not found" which I suppose makes perfectly good sense.
I know enough php to be able to break things, apparently, and my knowledge of Composer comes mostly from messing around with Flarum a little bit.
I'm sure I'm missing something obvious here, I'm guessing involving namespace declarations or something, but I can't put the pieces together.
I'm looking suspiciously at my composer.json file, as well—something doesn't seem right, but I'm not sure what to fix.
For the record, my composer.json, composer.lock, and vendor folder are in my theme folder with functions.php. My composer.json file looks like this:
{
"require": {
"emilianozublena/sheetsu-php": "^0.0.6"
}
}
and I'm not sure it should.
But what's more troubling is finding a way around that use Sheetsu\Sheetsu line that seems to totally break Wordpress...
Thanks for any help!
I assume here you have installed the package using command line running composer install or composer require emilianozublena/sheetsu-php.
You cannot use the use php keyword inside function. The use keyword must be declared in the outermost scope of a file (the global scope). Refer to this answer for more detail here
So, in this condition you can chain the namespace while instantiating your class. In our case new Sheetsu(['sheetId' => '8b297aa81110']) becomes new \Sheetsu\Sheetsu(['sheetId' => '8b297aa81110']);
Try this code below
function do_sheetsu() {
require('vendor/autoload.php');
$sheetsu = new \Sheetsu\Sheetsu(['sheetId' => '8b297aa81110']);
$response = $sheetsu->search(['id' => '2.05.1']);
$collection = $response->getCollection();
echo $collection->get(0)->answer;
}
add_shortcode('sheetsu','do_sheetsu');

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.

Missing includes in Wordpress plugin (wp_insert_post function)

I created a code which generates content and I want to populate Wordpress's database with it. I am using wp_insert_post and add_post_meta functions but every time I load which triggers the script I get this error:
Fatal error: Call to undefined function add_action() in \www\scriptmafia\wp-includes\post.php on line 153
To my mind it due to some missing includes. I have included post.php which it mentioned as required include for wp_insert_post function.
I know this is pretty common among newbies in WordPress. However, any solution I have tried have not worked for me.
require_once( ABSPATH . '/wp-load.php'); should solve it :)

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.

Resources