Php form doesn't work until second button click - php

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>

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

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>

Execute A Function from Functions.php in Form

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

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>

Submit button to affect multiple files

Current Situation:
So, for an article page (article.php), I have 2 different sections (title, content), which are hooked to the article page from their own php files (Title is pulled from title.php while content is pulled from content.php).
Now, I added an ability for the author to edit the title and content on the article which they need to press submit button in order save the change.
Problem
After editing, the author needs to press "Submit" button in order to update the data.
However, when I put the "submit" button in either title.php or content.php, only the corresponding section is updated while the other is unchanged (for example, if I have this submit button in the title.php then only title is edited).
If I put the button in the article.php then of course neither the title nor content gets updated.
Here is the mark up:
Article.php:
<div class="article">
<?php do_action ('article_summary'); ?>
</div>
Title.php (with the submit button, which only updates the title)
<form role="form" method="post">
<div class="edit_title">
<input type="hidden" value="<?php echo $post_id; ?>">
<?php post_input_box( $post_id, 'post_title', array( 'placeholder' => 'Article title..', 'value' => $post->post_title ) ); ?>
<div class="update-button-wrap">
<input type="submit" name="update_product" value="<?php esc_attr_e( 'Update', 'site' ); ?>"/>
</div>
</div>
</form>
Content.php
<form role="form" method="post">
<div class="edit_content">
<?php post_input_box( $post_id, 'post_content', array( 'placeholder' => 'Short description..', 'value' => $post->post_content ), 'textarea' ); ?>
</div>
</form>
Does anyone know how the submit button can save both title and content that are in two different files?
Thanks bunch!
Try using a super-button (pattent pending) that presses all them other buttons for ye...
<button type='button' onclick="$('[type="submit"]').click()">Submit</button>
Since its type button, it won't submit itself. Replace each submit button with this. I mean, leave the original buttons there, but style them with
button[type="submit"] { display:none }

Categories