submit to ninja form programmatically - wordpress

I've been researching for days and all I want to be able to do is create entries in the ninja form admin listings. Either by submitting custom form (not ninja form generated) or just calling a hook and passing data (data will match actual form fields created in ninja form).
I want to be able to do this so that I can create any type of form layout and still be able to submit to ninja form entry. Or if anyone has any other information on a plugin that can allow me to do such a thing, please share.

In NinjaForms version 3, you probably want to look at this file:
ninja-forms/includes/Actions/Save.php
The process function contains the important bits that may help you:
$sub = Ninja_Forms()->form( $form_id )->sub()->get();
foreach($fields as $field_id => $field_value){
$sub->update_field_value( $field_id, $field_value );
}
$sub->save();
In NinjaForms version 2, it is a little different
$sub_id = Ninja_Forms()->subs()->create( $form_id );
foreach( $form_fields as $field_id => $value ) {
Ninja_Forms()->sub( $sub_id )->add_field( $field_id, $value );
}
Where the $form_fields array would look like:
$form_fields = array(
$fiel_id_1 => $value_1,
$fiel_id_2 => $value_2,
...
);

I know this is is quite a while after the last comment on this post, but it should be noted that if you submit the form this way you will not be able to trigger any notifications. I have spent hours upon hours trying to include different classes and functions and try to rewrite the notifications class locally in my webhook script that was processing my forms, but I can't get it to work. Maybe there is someone smarter than me that can figure it out or maybe one day Ninja Forms will properly build out this functionality in their API. But for right now, if you need notifications this is not a full solution.
I would recommend switching over to Gravity Forms if you are still early enough in your project. Their API does have a submit_form() function that also triggers notifications and action steps. I just wish they would spend some time on their absolutely atrocious interface. I hate everything about their admin UX, but for more complex development things like this, they really are the best option.
Documentation

Related

Cron Wordpress Update four post every hour

I have function that update post content automatically based from custom field like this:
function update_content(){
$mycustomfield = get_post_meta( get_the_ID(), 'customfield', true);
$post = array();
$post['ID'] = get_the_ID();
$post['post_content' ] = $mycustomfield ;
$post['post_title' ] = $mycustomfield ;
// Update the post into the database
wp_update_post( $post );
We update only custom field to make content. For now, we launch this function manually on save_post hook, but the articles are so many and we need now a cron to automate this function: process 4 posts every hour until all posts are completed, then start over.
How to make this, thank you
WordPress actually has a built-in psuedo cron system called WP Cron. It doesn't function exactly like a proper server cron, but can perform a similar function in many cases. You can find documentation on it here:
https://developer.wordpress.org/plugins/cron/#:~:text=WP%2DCron%20is%20how%20WordPress,post%2C%20utilize%20WP%2DCron.&text=WP%2DCron%20works%20by%20checking,what%20needs%20to%20be%20run.
However thinking about your use case and looking at your function above, I'm wondering what the purpose of your cron is? It looks from that function like all you're doing is taking some content already in your database and putting it somewhere else. Why? Why not simply display your custom field in the correct spot? Or better yet, use the fields as intended?
Even if that is necessary - maybe I don't understand fully from the example above - I think your initial inclination to run this on save_post is much more correct. Unless there's some information external to your site that's changing, the only time these values will change is when you save the post. So what is the purpose of running it on a schedule?

WooCommerce - How To Update 'Net Sales' Only When Payment is Complete

I have a WooCommerce set up, whereby all orders are added manually.
When I add a (pending) order, the WooCommerce order status hook registers the sale (for reporting).
I wish the switch this process to the hook called only when the order is (again manually) set to 'complete'.
There are a couple plugins ( eg. https://docs.woocommerce.com/document/woocommerce-order-status-control/ / https://wordpress.org/plugins/advanced-reporting-for-woocommerce/ etc ), but these are either overkill or simply don't provide this functionality..
I've also found a couple related posts, essentially describing overriding the woocommerce hooks to this end ( Getting order data after successful checkout hook etc, but unfortunately whilst the solutions correspond ( ie adapting the correct hooks - the context differs ).
I'm reluctant to prevent functionality in these hooks when attempting to overwrite/reorder the actions so any pointers which hooks I can use to achieve this would be really helpful.
Many thanks!
maybe you can try to use the ... filter, something like (untested):
add_filter( 'woocommerce_reports_order_statuses', 'fc_order_status_reports', 20, 1 );
function fc_order_status_reports( $statuses ) {
$statuses = array('completed');
return $statuses;
}
The code snippet is to add to your active theme's functions.php file.
Let me know if it does the job ;)

How bad is $wp_rewrite->flush_rules() on 'init' action hook?

According to the Codex:
[...] This function can be extremely costly in terms of performance. It should be used as sparingly as possible - such as during activation or deactivation of plugins or themes. Every attempt should be made to avoid using it in hooks that execute on each page load, such as init.
Ok, so, I know it shouldn't be used in every page load and I'm not, but I still need a very conditional rewrite rule on this project.
I have these 2 url structures:
- example.com/products/tables/fancy-computer-table //This is a product url, here I expect the normal behavior from wp.
- example.com/products/tables/office //This should be some kind of a filter, where the site gets all the tables related to the office department. Note how both URL structure matches.
To make it work, I narrowed it down to these very specific URLs, using some regex. If matched, I'll run 2 queries to verify if the url I'm in is for the filter I want that rule to apply, or if it's a product url. In the latter, I want wordpress to behave normally. But I have to flush the rules either way, whether it's a product or it's the category and the filter, so that both pages work properly and dinamically.
I do all this to narrow down the use of this function to the least possible, but the Codex doesn't really tell me how bad it does affect the performance, or why.
I'm passing the parameter on that function as false by the way, so it doesn't rewrite my htaccess file, but I'm not sure where that rewrite rule option is stored, if it's on the memory somewhere, or on the database, wish they would provide some clarification on that.
Thanks in advance for reading this question. Any pointers are appreciated here, even if you want to point me to some other way of doing this. :)
EDIT: Posting some code here so you guys can actually understand what I mean and let me know if this maybe this could be bad practice... maybe a suggestion on how to do it better.
<?php function custom_rewrite_products()
{
// regex to match either "products/tables/fancy-computer-table" or "products/tables/office"
preg_match('#products\/([^\/]+)\/([^\/]+)\/?#', $_SERVER['REQUEST_URI'], $url_matches);
if(is_array($url_matches)) {
$query_category = new WP_Query('category_name='.$url_matches[1]);
$query_category->get_posts();
$args = array(
'post_type' => 'filters',
'name' => $url_matches[2]
);
$query_post = new WP_Query($args);
$query_post->get_posts();
if($query_category->have_posts() && $query_post->have_posts()) {
$category_ID = '';
$filter = '';
$category_ID = '' . $query_category->query_vars['cat'] . '';
$filter = '' . $query_post->query_vars['name'] . '';
$string = "index.php?cat={$category_ID}&filter={$filter}";
add_rewrite_rule('products/([^/]+)/([^/]+)/?$', $string, 'top');
}
global $wp_rewrite;
//Call flush_rules() as a method of the $wp_rewrite object
$wp_rewrite->flush_rules(false);
}
}
add_action('init', 'custom_rewrite_products');
I still don't understand why, but the flush is needed here, so the $category_ID and $filter variables actually get passed to the rewrite rules. If I take it out, the rewrite just goes to the index page, the place where the values should be are empty.
In case you're wondering, I created the filters on a custom post type and they are related to each post by using a custom field, since they're like four or five and they're present on every category on this project. As you can see, I already have a category/subcategory structure and I didn't find it smart to create these filters once again by hand inside each main category.
There is a 'rewrite_rules' record in the 'wp_options' table that includes all the rewrite rules. When calling flush_rules(), WordPress will clear this record and regenerate all the new rules, including the ones you're not changing.

WP Post Meta Tags not working as expected

I have been trying to associate CUSTOM data to each of my WP POSTS using the following piece of code :
if($condition === true){
if ( ! update_post_meta ($post_id, '_someData', $someVariable) ) {
add_post_meta($post_id, '_someData', $someVariable);
}
}
However, seems like the META VALUE is RESET to default i.e. Zero OR Blank. Our WordPress website has around 40 plugins, and I think one of these WordPress plugins, is coming in my way of doing things. All of my logic works fine, on a demo WordPres website. Is there a way for me to have total control to set the META Value for a given POST ? Also, is there a way where I can be notified that the META Key is about to change and then I can decide whether OR not to change the Meta Value ?
Any pointers or reference URL's can be of great help. Thanks !
You only need to call update_post_meta in either scenario. Calling add_post_meta() is not necessary and could be causing this problem.
From the codex:
The function update_post_meta() updates the value of an existing meta key (custom field) for the specified post.
This may be used in place of add_post_meta() function. 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.

Wordpress Plugin - Self-Hosted Update

I'm developing a Wordpress plugin that requires updates, although the plugin version is being checked from my server. There are several plugins that I have developed which use the exact same server to check for new versions. The problem I'm experiencing is that when all the plugins require an update and I click View Details, one of the plugins will show details of the update (version, description, and etc), but the other plugins won't show any information. After some debugging I can see that the server is returning data for sure.
My question is, how can I apply the plugins_api filter multiple times without it conflicting with the other plugins?
Your observation is right. It is not obvious. Even the book of Brad and Ozh (Plugin development ed. Wrox) includes an error in the example on page 267 in the chapter "make your own API repository".
Like you, I spent (lost) time to find the issue with a two plugins in alternate API...
The solution:
Remember that that first parameter in the WP filter is the original value passed to the filter.
So to concatenate the filters (listed by plugins using alternate api)... the first line must be:
function xiliw_altapi_information( $false, $action, $args ) {
$plugin_slug = plugin_basename( __FILE__ );
// Check if this plugins API is about this plugin
if( $args->slug != $plugin_slug ) {
return $false; // var to conserve the value of previous filter of plugins list in alternate api. fixes book error not val false
}
// POST data to send to your API
$args = array(
'action' => 'plugin_information',
'plugin_name' => $plugin_slug,
'version' => $transient->checked[$plugin_slug],
'registration' => $this->registration
);//../..
By doing this test, each time the list of hooks is called, only one - the concerned plugin - gives the right answer to display the information for the splash window.
If I have time, I probably will publish soon a more complete article about a class to manage this alternate powerful API and how to add it to a -private- plugin.

Resources