register_acivation_hook function not working - wordpress

I'm learning the basics of creating a wordpress plugin. A function named as register_activation_hook is used to execute a specific function when the plugin is activated. I tried to use it, but it is not working. What am i doing wrong?
register_activation_hook(__FILE__, 'kk_aw_install');
kk_aw_install(){
echo "<script>alert('working')</script>";
}

Try to use:
register_activation_hook(__FILE__, 'kk_al_install');
since your function name is kk_al_install not kk_aw_install

Related

Replacing a core WordPress function

I am trying to replace the core WP function wp_delete attachment. In my functions.php file I am adding the following code:
add_action( 'init', 'remove_my_action' );
function remove_my_action(){
remove_action( 'delete_attachment', 'wp_delete_attachment' );
add_filter('delete_attachment','wp_delete_attachment',10,2);
}
And then the copy of the replaced function goes here with edited code.
This does not work. I get the error: Cannot redeclare wp_delete_attachment(). I've tried a number of other methods, but can't get it to work.
The bottom line is that I need to add some code to the middle of wp_delete_attachment function. If I can replace that function with my version somehow or add the code to the existing function without editing the actual code in the wp-includes/post.php file (so that version updates don't override my code), I would be satisfied with either. How can this be done? All the options I found through questions have not solved the issue. Thanks!!
You'll need to name your copy of wp_delete_attachment to a unique name. Perhaps namespace it with your site name like function my_site_wp_delete_attachment().
Also, I believe you'll need to use add_action instead of add_filter.
remove_action( 'delete_attachment', 'wp_delete_attachment' );
add_action( 'delete_attachment', 'my_site_wp_delete_attachment');

Call to undefined function add_options_page()

I developed the plugin for my wordpress project. I successfully tested it on my local xampp server with 5.3 php. Then I uploaded my project to the web hosting with php 5.2. First trouble which with I faced off was unsupporting anonymous functions in php 5.2. No issue, I redeclared all functions with names. But then I got error Call to undefined function add_options_page(), which I counldn't explain. Plz help me guys with your advices
My part of code:
function mainPage(){
///some code
}
function mainPage2(){
add_options_page('Submissions of MAIN page contact form', 'Submissions of MAIN page contact form', 'manage_options','ea_submissions2', mainPage());
}
add_action('admin_menu',mainPage2());
I think something wrong with my funcitons, look through it please.
There is no issue with php 5.2 as I thought, this part of code also doesn't work with php 5.3! Something wrong with my code
I had a similar problem, turns out I was running a function too early:
Use admin_init hook instead of init
Hopefully that helps someone out :D
This doesn't work because you have normal function not wrapped in a class, and because add_options_page does not work yet by that time that is why you get the error.
A fix would be to use an anonymous function in the add action call, but hence that does not work on php 5.2.
So long story short, this is fixable though, but you shouldn't run php 5.2 anymore in the first place. PHP 5.5 is already in development and 5.3 is facto standard these days. A solution is to ask your hosting company to upgrade php to at least 5.3 so that you can use anonymous functions and you can hook it to the add action call.
Or, wrap it all in a class and on the admin init function create the new class.
You have to call your code inside admin_menu like this
add_action( 'admin_menu', array(&$this, 'addWidgetSettingsMenu' ));
Aware that this is an old question, this is your problem:
add_action('admin_menu',mainPage2());
Here you are invoking the mainPage2() function and adding the return value of that function as an argument to the add_action method.
You should do
add_action('admin_menu', 'mainPage2');
This way, the mainPage2 function will be called when admin_menu happens.
Try it without giving space in title :-
SubmissionsOfMainPageContactForm

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 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 URL rewrite not working/registering

I've been trying to get my head around Wordpress URL rewrites, but I'm having no luck.
What I want to do:
I building a custom plugin where a user can build products from various options. The options collectively build a code which refers to the unique product the customer has built.
The code might be something like 140-3-WPA-ABC-2.
The plugin will appear on a single dedicated page:
http://wordpress-site/configurator/
I want a customer with a prexisiting code to be able to enter it into the url like this:
http://wordpress-site/configurator/140-3-WPA-ABC-2/
Whereupon, the plugin gets the variable, and uses it to build the correct product.
Problem
It should be fairly simply but I can't get anything to work using the Wordpress URL rewriting rules, I can't even get anything to seemingly get registered as a Wordpress query var.
I've been trying the following in the main plugin initialisation code:
add_filter( 'query_vars', 'conf_query_vars' );
add_action( 'init', 'cong_rewrites' );
function conf_query_vars($query_vars){
$query_vars[] = 'product_code';
return $query_vars;
}
function conf_rewrites(){
add_rewrite_rule(
'configurator/([^/]+)/?$',
'index.php?product_code=$matches[1]',
'top'
);
}
If I then try and open http://wordpress-site/configurator/140-3-WPA-ABC-2/ I get a page not found error. Echoing query_vars seems to show the variable 'product_code' is not created.
ps I've tried flushing the rewrite cache. Apologies for cross-posting to Wordpress.stackexchange.com - but seems programming question better here?
Try this, I had the same problem.
add_action('init', function() {
add_rewrite_endpoint('sponsor', EP_ALL);
});
add_filter('request', function($args) {
print_r($args);
return $args;
});
I think you should just integrate the function add_rewrite_endpoint.

Resources