I am developing a wordpress plugin using the wordpress boilerplate template. Additionally, I am using Redux Framework as an options framework. I load the options-init.php in the load_dependencies() function of the main include file.
Everything works fine. However, when calling wp_create_nonce in options-init.php I get a Fatal error: Uncaught Error: Call to undefined function wp_create_nonce()
Why is it and how to solve it?
wp_create_nonce() is a pluggable function loaded after plugins are loaded.
BEFORE call pluggable.php file:
require_once( ABSPATH . 'wp-includes/pluggable.php' );
Thanks.
I found the solution myself, although not sure if it is correct/best practice.
I moved the include of the redux framework into a seperate class which I call via action hook on ''plugins_loaded'
//Initiate Redux Framework
$this->loader->add_action( 'plugins_loaded', $plugin_admin_redux, 'init_redux_framework' );
Inside the method init_redux_framework() I just put the require once to the admin-init.php
Interestingly though that adding it on 'init' instead of 'plugins_loaded' was too late for Redux to be initialized.
Why do you need wp_create_nonce() inside plugin?
Nonce is usually included in a hidden HTML form field or as a part of an URL and therefore sent with a request by submitting a form field or visiting a link:
<?php
// Create an nonce for a link.
// We pass it as a GET parameter.
// The target page will perform some action based on the 'do_something' parameter.
$nonce = wp_create_nonce( 'my-nonce' );
?>
<a href='myplugin.php?do_something=some_action&_wpnonce=<?php echo $nonce; ?>'>Do some action</a>
And inside plugin files you can verify nounce:
wp_verify_nonce( $nonce, 'my-nonce' );
wp_create_nonce() is a pluggable function loaded after plugins are loaded.
Be sure to call your class method on proper hook, 'init' (or later) is a good place: once your function output something (a form) there is no reason to run earlier than that.
wp_verify_nonce() receives 2 arguments:
$nonce
$action
1st argument is the nonce, and comes from the request
In fact, wp_verify_nonce() have to be used like so:
// here I assume that the form is submitted using 'post' as method
$verify = wp_verify_nonce($_POST['message-send']);
So the first argument passed to wp_verify_nonce() is exactly the value that is present in the hidden field
2nd argument: the wp_create_nonce() method
Regarding the second argument, it depends on how you build the nonce value.
E.g. if you did:
<?php $nonce = wp_create_nonce( 'custom-action' ); ?>
<input type="hidden" name="message-send" value="<?php echo $nonce ?>" />
Then you need to do:
$verify = wp_verify_nonce( $_POST['message-send'], 'custom-action' );
So, the second argument is what was used as argument to wp_create_nonce().
2nd argument: the wp_nonce_field() method
If you created the nonce using wp_nonce_field() like:
wp_nonce_field( 'another_action', 'message-send' );
Then you need to verify the nonce like so:
$verify = wp_verify_nonce( $_POST['message-send'], 'another_action' );
So, this time, the action is whatever passed as first argument to wp_nonce_field().
Recap:
To pass wp_verify_nonce() validation you need to pass 2 arguments to the function, one is the value in the nonce hidden field, the other is the action, and depends on how the nonce value was built.
Related
lets say this filter for example:
apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id );
I will hook on it like this:
function my_func($wut,$huh){
//...my code...
}
add_filter('wp_generate_attachment_metadata', 'my_func' );
How can I access the $metadata and $attachment_id from the initial apply_filter call in my function?
Thank you #Samvel for your comment, that totally works.
As seen on this page: https://developer.wordpress.org/reference/functions/add_filter/ I needed to add that 4th argument in my add_filter and set it to 2 to allow a second argument to pass in, otherwise it was locked at 1. Thats why I couldnt retrieve that second argument!
I want to call my form from my template but I don't want to use IDs (since my form will have different IDs on different servers). Is there a way to call a form from its name rather than its ID?
EDIT: to clarify, in my template instead of calling the form using for instance echo do_shortcode('[mc4wp_form id="7"]'); (which won't work on another server where the same form has another ID) I'd like to call it by its name using a command of that kind: display_mailchimp_form('android_user_signup'); where 'android_user_signup' is the name of my form. That way even if the same form has different IDs on different servers, as long as its name is the same the form will successfully be called.
try override shortcode with your own function, in your function lookup the id by attribute you passed in like "name", then call the original function.
[updated per Shawn's request, here is a piece of code]
<?php
$post = get_page_by_title( 'title', OBJECT, 'mc4wp-form' );
if ( $post ) {
$id = $post->ID;
echo do_shortcode('[mc4wp_form id="$id"]');
}
?>
I was creating a wordpress plugin where the user enters in some information, and is able to generate shortcodes. I was not quite sure where the shortcodes are supposed to go - my current setup is class-based, and I want to be able to create a shortcode when an AJAX request is being made, and is successful. The following two methods are in the same file in the class.
This method gets called via the admin-ajax.php file:
public static function processAjax()
{
global $wpdb;
$event_data = $_POST['obj'];
$event_title = $event_data[0];
$event_subdomain = $event_data[1];
$result_events = $wpdb->get_results("SELECT * FROM wp_shortcode_plugin WHERE subdomain = '{$event_subdomain}'", OBJECT);
if (sizeof($result_events)>0) {
echo "duplicate";
} else {
add_shortcode($event_subdomain, 'getEmbed');
$results = $wpdb->insert('wp_shortcode_plugin', array("event_name"=>$event_title, "subdomain"=>$event_subdomain));
echo json_encode($_POST['obj']);
}
die();
}
And here is my getEmbed() method that I would like to call.
public static function getEmbed()
{
return 'test';
}
It seems that the shortcodes are not being created, however. Am I missing something here? Also, is it possible to pass a value to the getEmbed function from the add_shortcode() method?
Instead of adding shortcode directly from AJAX, you should use update_option to store in the information for the shortcode to be loaded. If option doesn't exist, it will be created.
Than you will simple use wp_init hook to load up all of the shortcodes you need to load in the function.php file for the theme or plugin php file.
You should use get_option within the wp_init hook and check the values in there. You will need to have function(s) associated with the shortcodes, which can be autogenerated in php using create_function or you can route them through 1 function (defined in your php file) that will have the $atts and $content parameters defined and do whatever depending on the value of your get_option that you send to that function.
add_shortcode function should be defined within the wp_init hook, after checking the value of the get_option function. You will need to give your option a name and add to it via the ajax function. the option will most likely want to be an array, that wordpress will automatically serialize. Than you use that array returned from get_option to loop through the array of shortcodes, and call add_shortcode as many times as you need there. This requires setting up your option array so that it has a shortcode tag defined in each index of the array. I would, personally, make the shortcode tag the key of the array and all attributes of the shortcode, imo, would than be an array of that array.
Hope this helps you to get started on this.
http://localhost/wordpress/give-api/forms/?key=15443f18029e6f5d3b65d04e1640ffbe&token=c3de770a410282359413c74a588c5c74
The above link is a plugin api link. Above link won't work to your browser.
when I set the above link in the browser , it returns array object like http://postimg.org/image/6ozmjy0e7/ .
My question is , how can I set this url in a variable in wordpress and how can I get the data from that array object. I just want to get the data from that array object. If any other process is available, then please suggest me. Thanks...
In functions.php:
function displayApiUrl() {
global $apiUrl; // you probably don't actually need to set it global as it is a function
$apiUrl = 'http://localhost/wordpress/give-api/forms/?key=15443f18029e6f5d3b65d04e1640ffbe&token=c3de770a410282359413c74a588c5c74';
return $apiUrl;
}
In your theme you can now use:
<?php $api = displayApiUrl(); ?>
With that you can process your array in a foreach loop:
<?php
$json_url = file_get_contents($api);
$json_data = json_decode($json_url, true);
foreach ($json_data['forms'] as $form) {
$form_id = $form['info']['id'];
echo $form_id;
}
?>
The new "standard" for WordPress rest apis is Json Rest API, which will be partially integrated into WordPress core in the next release.
You can get it here https://wordpress.org/plugins/json-rest-api/ and documentation at http://wp-api.org/
In terms of the question how to put array information into the URL, the format is
http://www.example.com/wp-json/endpoint?array_1[key1]=Pensacola&array_1[key2]=Florida
The URL of course changes, and the wp-json/endpoint is replaced with whatever the final endpoint is for which ever rest api you choose to use.
I am using add post meta function to save some data and its not working
<?php
//include '../../../wp-blog-header.php';
$unique = "true";
$pageID = $_GET['postID'];
echo "pageID:";
echo $pageID;
echo "</br>";
$num_posts = $_GET['num_posts'];
echo "num_posts: ";
echo $num_posts;
echo "</br>";
$num_posts_meta_key = "num_posts";
add_post_meta($pageID, $num_posts_meta_key, $num_posts , $unique) or update_post_meta($pageID, "num_posts" , $num_posts);
?>
Can someone help me out?
In first page I am getting all values from textboxes or checkboxes in javascript and then i am passing it in URL to next page where add_post_meta function is there.
I tried using method POST ...but then it doesnt work for me. It just submit the page and come back w/o doing anything on 1st page. I tried with GET method..but nothing works.
Hence I decided to take all values like num of post, post id in javascript and then from there pass it with url by using window.location.
I am very new to wordpress plugin coding. I thought POST method in my plugin is conflicting with some other post method in post.php..not sure though..
I am writing plugin for admin panel.
not sure what your problem is.. are you sure you're passing the right postID parameter? does the post exist in the database?
You don't really need to do add_post_meta() or update_post_meta.
From the manual:
The first thing this function will do
is make sure that $meta_key already
exists on $post_id. If it does not,
add_post_meta($post_id, $meta_key,
$meta_value) is called instead and its
result is returned.
<?php
// This minimum code should work, though you should really check that a post
// with this id does exist.
update_post_meta($_GET['postID'], "num_posts" , $_GET['num_posts']);
?>