Execute A Function from Functions.php in Form - php

I'm building a form which displays a certain piece of user_meta data and I want to be able to update this usermeta on form submit.
This is what I have at the moment;
function my_form_funnction() {
global $current_user;
$vat_no = get_user_meta( get_current_user_id(), 'vat_number', true );
?>
<form name="setPrices" action="" method="POST">
<label for="lowPrice">Vat Number: </label>
<input type="text" id="vat_number" name="vat_number" value="<?php echo $vat_no ?>" />
<button type="submit">Save</button>
</form>
<?php update_user_meta( get_current_user_id(), 'vat_number', esc_attr($_POST['vat_number']) );
}
But the thing is the php that updates the user meta does so when the page loads, I only want it to do it when the user pressess save.

You need to use Ajax to update information on a page without refreshing it. Try jQuery! It is a realy simple way to use Ajax. For example:
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<button onclick="javascript:la()">Save or do smth</button>
<div id="a">Here will be included the loadable thing</div>
<script>
function la(){
$("#a").load("/functions.php?func=my_form_function");
}
</script>
And in functions.php you may set:
if(isset($_GET["func"]){
if($_GET["func"] == "my_form_function"){
my_form_funnction();
}
}

Related

How to change the posts category with a button click

I am trying to create a button "Approved" to change the post category from it's current one to the "approved" category. I don't mind if it reloads the page or not. I would also like to redirect the page afterward to the next post in the original category.
I have found some questions on this already but am ultimately lost on how to get this all together and working.
<?php add_shortcode('approved_button', 'brist_approved_button_function');
function brist_approved_button_function() {
ob_start(); ?>
<form method="post" action="approved.php">
<input type="submit" value="Approved" name="submit"> <!-- assign a name for the button -->
</form>
<?php
wp_set_object_terms( $post_id, intval( $_POST['approved'] ), 'category', false );
$output = ob_get_clean();
return $output;
}?>
I figured it out. Though it was a head-scratcher for me. If anyone else if having the same issue, note the code below. You have to create the select and then have the 'selected' option on the category you want the post to change to. Then, in the CSS, hide the select input, only leaving the button.
<?php add_shortcode('approved_button', 'approved_button_function');?>
<?php function approved_button_function() { ob_start();?>
<div class="approval">
<form action="" id="update-post" method="post">
<?php wp_dropdown_categories( "selected='categoryId'&exclude=21&class=approval-select&show_count=1&hierarchical=1&orderby=name&order=ASC&&hide_empty=0&show_option_all=Choose An Option" ); ?>
<input class="approval-button" type="submit" name="submit" value="Approve" />
</form>
</div>
<?php if ( array_key_exists('cat', $_POST )) {
global $post;
$post_id = $post->ID;
wp_set_object_terms( $post_id, intval( $_POST['cat'] ), 'category', false );
} ?>

Hook for post-Wordpress/Woocommerce registration to check form data & add function

What I have:
A log in page created by WooCommerce
A check box added to it by MC4WP (Mailchimp for Wordpress)
Roles managed by Members plugin
What I am trying to do:
When a user registers, if they have selected the check box, I would like to assign them an additional role. (NOT update their role from the default woocommerce Customer to something else; trying to add a role in addition to that).
I have assigned multiple roles with another form successfully, but without the added step of the checkbox or woocommerce's form.
I've tried hooking user_register, profile_update, even woocommerce_created_customer, but none so far have worked.
I'm not sure where my issue is, but this I have found:
When including the checkbox name as taken from source code, I get a 500 error after form submission. User is registered (with customer role only).
If I put in a junk name there, no error.
I'm not sure what I've misunderstood (or just completely missed).
Any help/thoughts are appreciated!
//add_action( 'user_register', 'check_the_form_box_add_role');
//add_action('woocommerce_created_customer', 'check_the_form_box_add_role');
add_action('profile_update', 'check_the_form_box_add_role');
function check_the_form_box_add_role( $user_id ) {
if ( isset($_POST['_mc4wp_subscribe_wp-registration-form']) ) {
$user_id = wp_insert_user( $user_id );
$role_to_add = 'thisistherole;
$user_id->add_role($role_to_add);
}
}
The form from source code:
<form method="post" class="register">
<p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
<label for="reg_email">Email address <span class="required">*</span></label>
<input type="email" class="woocommerce-Input woocommerce-Input--text input-text" name="email" id="reg_email" value=""/>
</p>
<div style="left: -999em; position: absolute;"><label for="trap">Anti-spam</label><input type="text" name="email_2" id="trap" tabindex="-1"/></div>
<input type="hidden" name="_mc4wp_subscribe_wp-registration-form" value="0"/><p class="mc4wp-checkbox mc4wp-checkbox-wp-registration-form"><label><input type="checkbox" name="_mc4wp_subscribe_wp-registration-form" value="1" checked="checked"/><span>This is text for checkbox!</span></label></p>
<p class="woocomerce-FormRow form-row">
<input type="hidden" id="_wpnonce" name="_wpnonce" value="994ca0c618"/><input type="hidden" name="_wp_http_referer" value="/my-account/"/> <input type="submit" class="woocommerce-Button button" name="register" value="Register"/>
</p>
</form>
I thought about hooking registration_errors, but this runs before the user is created. Previously, I would create user as Customer and use something like
$user_id = wp_insert_user( $user_id );
$role_to_add = 'thisistherole;
$user_id->add_role($role_to_add);
to add the additional role.
If anyone needs it:
I was correct previously hooking user_register.
The issue was I was not getting a WPUser object and was thusly calling add_role() when it wasn't available. Passing the id to get_userdata seems to have solved it.
add_action('user_register', 'name_of_function', 10, 1);
function name_of_function( $user_id ) {
if ( isset($_POST['_mc4wp_subscribe_wp-registration-form']) &&
$_POST['_mc4wp_subscribe_wp-registration-form'] == '1')
{
$useridid = get_userdata( $user_id );
$role_to_add = 'thisistherole';
$useridid->add_role($role_to_add);
}
else
{
//do nothing
}
}
More on get_userdata: https://codex.wordpress.org/Function_Reference/get_userdata
and on user_register:
https://codex.wordpress.org/Plugin_API/Action_Reference/user_register

get_user_meta and update_user_meta on the same page

I have a little form that updates a custom user meta I made named "Doel":
<?php $user_ID = get_current_user_id(); $user_data = get_user_meta($user_ID); ?>
<form method="POST" action="" id="doel_edit">
<input type="text" value="<?php if(isset($user_data['doel'][0])): echo $user_data['doel'][0]; endif; ?>" name="doel">
<button value="done" type="submit" form="doel_edit">Opslaan</button>
</form>
When I submit the form I get the standard update_user_meta() function to run
<?php if(isset($_POST['doel'])) {
update_user_meta($user_ID, 'doel', $_POST['doel']);
} ?>
No this all works fine, but when the page loads after submitting the form it echo's the old value from before update_user_meta() ran. I have to reload the page by hand to get my new value.
Why doesn't the value update directly? How can I fix that?
I think running functions like these in the wp-admin works fine.
I actually found the answer with help from somewhere else.
Because I ran get_user_meta() before update_user_meta() the values didn't update. Once I placed update_user_meta() after get_user_meta() it worked:
<?php $user_ID = get_current_user_id(); ?>
<?php if(isset($_POST['doel'])) {
update_user_meta($user_ID, 'doel', $_POST['doel']);
} ?>
<?php $user_data = get_user_meta($user_ID); ?>
<form method="POST" action="" id="doel_edit">
<input type="text" value="<?php if(isset($user_data['doel'][0])): echo $user_data['doel'][0]; endif; ?>" name="doel">
<button value="done" type="submit" form="doel_edit">Opslaan</button
</form>

Php form doesn't work until second button click

I'm working on a follow button so that when the user clicks the button it creates a object/term relationship with the author. Anyways i'm using the form below so that when the user clicks the button it executes the php code, anyways the code below works, but only when you click the button the second time. The first time it does nothing for some reason. Thanks, Julian
<?php
if(isset($_POST['select']))
{
$author_id = get_the_author_meta( 'nickname' );
$user_id = get_current_user_id();
$term_ids = wp_set_object_terms( $user_id, $author_id, 'user_follow', true );
}
?>
<html>
<body>
<form action="<?php echo esc_url($_SERVER['REQUEST_URI']); ?>" method="post">
<input type="submit" name="select" value="select"/>
</form>

How to delete comments programmatically in WordPress?

I want my bloggers to be able to delete comments via the front end instead of the standard way via the WP dashboard. I wrote a function custom_delete_post_comment() which deletes a comment with a given ID.
function custom_delete_post_comment() {
$comment_id = comment_ID();
wp_delete_comment( $comment_id, true )
}
As you can see, my function uses WordPress' wp_delete_comment() function.
I plan on having a button next to each comment which when clicked will run the delete function I wrote hence removing the comment.
I have come up with a solution using the $_POST approach. My question is how do I modify my code so that the page reloads to reflect the fact that the comment has been deleted?
<?php if( 'POST' == $_SERVER['REQUEST_METHOD'] ) {
set_query_var( 'commentid1', $_POST['commentid'] );
wp_delete_comment( get_query_var( 'commentid1'), true );
};
?>
<form class="delete-comment" action="" method="post">
<input type="hidden" name="commentid" value="<?php comment_ID() ?>" />
<input type="submit" value="Delete" title="Delete" class="btn" />
</form>

Categories