How to delete comments programmatically in WordPress? - php

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>

Related

Delete costum post type from frontend

i'm created a costum post type
i created a button to delete the post from frontend but it's redirecting me to the home page without deleteing the post (the post can be deleted only when i have admin role) i want to allow to the post owner to delete it too
this is my code
<?php if( is_user_logged_in() && is_author(get_current_user_id()) )
echo "</i>حذف ";?>
Adding the following code will let you delete post from front end in WordPress.
Method 1:-
<?php
$url = get_bloginfo('url');
if(is_user_logged_in() && is_author(get_current_user_id() && current_user_can('edit_post', $post->ID))){
echo '<a class="delete-post" href="';
echo wp_nonce_url("$url/wp-admin/post.php?action=trash&post=$id", 'delete-post_' . $post->ID);
echo '"><i class=\"fa fa-fw fa-times\"></i>حذف</a>';
}
?>
Method 2:-
<?php
if(is_user_logged_in() && is_author(get_current_user_id())){
echo '<i class=\"fa fa-fw fa-times\"></i>حذف';
}
?>
done,
i did some researche i found something like this
//function to print publish button
function show_publish_button(){
Global $post;
//only print fi admin
echo '<form id="myForm" name="front_end_publish" method="POST" action="">
<input type="hidden" name="pid" id="pid" value="'.$post->ID.'">
<button type="submit" name="submit" id="submit" value="حذف" class="btn" style="margin-left:2px;background:#f5f5f5;"><i class="fa fa-fw fa-times"></i>حذف</button>
</form>';}
then :
//function to update post status
function change_post_status($post_id,$status){
$current_post = get_post( $post_id, 'ARRAY_A' );
$current_post['post_status'] = $status;
wp_update_post($current_post);
}
if (isset($_POST['pid']) && !empty($_POST['pid'])){
change_post_status((int)$_POST['pid'],'trash');
}
past all code above in functions.php
then in the template file call the function show_publish_button(); to show the delete button
We solved your problem. The plugin developers at Business Entourage launched a plugin called Delete Post, which allows post author (and only post author) to delete post on the front end. It is great for blog/site owners and works incredibly. Click here to check it out -> Delete Post

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

Page reload ask to submit data over and over again

I have the following *code below, every time I refresh the page it asks me to send the form again. How do I avoid that, I don't want to resend a form on a page refresh. Thanks in advance.
*CODE
<?php function make_user_feedback_form() {
global $wpdb;
global $current_user;
$ufUserID = $current_user->ID;
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'updateFeedback' ) {
$ufDataUpdate = $wpdb->insert( 'wp_user_feedback', array( 'date' => current_time('mysql'), 'responses' => $_POST["test"]) );
}
}?>
<div id="form">
<ol>
<form method="post">
<li><label for="test">Question 01</label><input type="text" id="datepicker" name="test" value="" /></li> <!-- the (name="test") value is what the ('responses' => $_POST["test"]) value is talking too -->
<li><input name="submit" type="submit" id="submit" class="submit button" value="Send feedback" /></li>
<?php wp_nonce_field( 'updateFeedback' ); ?>
<input name="action" type="hidden" id="action" value="updateFeedback" />
</form>
</ol>
</div>
<?php
add_action('the_content','make_user_feedback_form');
?>
After you have processed the form data and stored it in the database or worked with it in some way, reload the same page using:
header("location: thispage.php");
Doing this will destroy the POST data and allow the page to be refreshed without displaying the resubmit alert.

Categories