Delete costum post type from frontend - php

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

Related

How to add a custom empty cart button on configurable product page:Magento

I have a configurable product with two option and i want to add an empty cart button on that page to empty the items:
i have copied the button from cart page and embed it but its not worked.
please suggest a way to add a button.
<button type="submit" name="update_cart_action" value="empty_cart" title="<?php echo $this->__('Empty Cart'); ?>" class="button2 btn-empty" id="empty_cart_button"><span><span><?php echo $this->__('Empty Cart'); ?></span></span></button>
You will need to create a new controller and then add the action of the controller to the button you just created.
Then create a model to empty the $quote which is the current items. Thats the basic theory.
Inchoo created something similar to what you want check it out here.
If you look at the updatePostAction function of Mage_Checkout_CartController, you should indeed be able to do that the way you want it. But right now your button is not linked to a form, so it does nothing.
But you will also need a valid form key for that to work.
Extract of Mage_Checkout_CartController :
public function updatePostAction()
{
if (!$this->_validateFormKey()) {
$this->_redirect('*/*/');
return;
}
$updateAction = (string)$this->getRequest()->getParam('update_cart_action');
switch ($updateAction) {
case 'empty_cart':
$this->_emptyShoppingCart();
break;
case 'update_qty':
$this->_updateShoppingCart();
break;
default:
$this->_updateShoppingCart();
}
$this->_goBack();
}
So this should be something working :
<form action="<?php echo $this->getUrl('checkout/cart/updatePost') ?>" method="post">
<?php echo $this->getBlockHtml('formkey'); ?>
<button type="submit" name="update_cart_action" value="empty_cart" title="<?php echo $this->__('Empty Cart'); ?>" class="button2 btn-empty" id="empty_cart_button"><span><span><?php echo $this->__('Empty Cart'); ?></span></span></button>
</form>

Wordpress submit a form, update link with form input and go to that link & link is "#"?

This code is a form that results in a link being created with variable $ui.
<form method="post" name="form" onsubmit="#">
Name search:
<input id="ui" type="text" name="ui" />
<input type="submit" class="Submit" value="submit" />
</form>
Then the variable $ui is used to set the value of the data-filter attribute. When you click the link (after the form submits) it goes to "#" which performs a sorting/listing function and reloads the page without refreshing.
<?php if(isset($_POST["ui"]))
{
$ui = $_POST["ui"];
}
$Filterclass = strtoupper(str_replace(" ", "-", $ui));
?>
<a href="#" id="gallery_filter"
data-filter=".<?php echo $Filterclass; ?>">
<?php echo strtoupper($ui); ?></a>
Right now, the form submits and then the link is created, then you can click on it.
It works as-is, but I'd like to have the 'submit' button just open that link, the one with newly updated data-filter attribute.
I would ask this is Wordpress.exchange but I think is too advanced.
I won't write the whole process but modify your existing code as below(assuming jquery is already loaded)
<?php if(isset($_POST["ui"]))
{
$ui = $_POST["ui"];
echo '<script type="text/javascript">
jQuery(document).ready(function(e){
jQuery("#gallery_filter").click();
});
</script>';
}
$Filterclass = strtoupper(str_replace(" ", "-", $ui));
?>
<a href="#" id="gallery_filter"
data-filter=".<?php echo $Filterclass; ?>">
<?php echo strtoupper($ui); ?></a>

zend link button element in controller

I have this button in my controller and trying to echo it back to the user in my view phtml but its seems not to be returning the element.
Button
$button = new Zend_Form_Element_button('button');
$button->setAttrib('id', 'B!');
$button->setLabel('Previous');
$button->setAttrib('onClick', "parent.location='" . Zend_Controller_Front::getInstance()->getBaseUrl() .'/../..' ."'");
$this->view->button = $button;
in my view
<?php echo $this->button; ?>
Your code work in my application.
If you just want a button, you can do something like that:
In your Controller :
$this->view->location = "parent.location='" . Zend_Controller_Front::getInstance()->getBaseUrl() .'/../..' . "'";
In the view
<input id="B" type="button" value="Previous" onClick="<?php echo $this->location;?>">

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>

How do I set a default query string in my URL?

I am trying to build a webapp which shall filter results based on the users current button select and slider choice, I will be achieving this via PHP, SQL and AJAX calls but I need help with sending the correct data to my URL.
DB Dip -
$pType = $_GET['s'];
$fPsiVal = '15000';
$fGpmVal = '10000';
$hPsiVal = '30000';
$hGpmVal = '10000';
$sql = "SELECT * FROM pumps
WHERE pump_type = '$pType'
AND flow_psi <= '$fPsiVal'
AND flow_gpm <= '$fGpmVal'
AND high_psi <= '$hPsiVal'
AND high_gpm <= '$hGpmVal'";
I want to set a default value for pType of ?pType=CONTINUOUS instead of having a static variable, how would I pass this to my URL when the page is called initially?
In order to handle user result filtering I created a form with the method of GET which as I currently understand should send the data to the URL upon for submission, however nothing happens and I am not quite sure what I have done wrong
GET Form -
<form name="pumpCat" action="?s=<?php echo $pType ?>" method="get" align="center">
<div class="btn-group" data-toggle="buttons-radio">
<button type="submit" class="<?php if( $pType == 'intermittent' ){ echo 'active '; } else echo 'noClass '; ?>btn btn-primary waitingUiBut" id="but1" name="s" value="intermittent" action="pumpCat">INTERMITTENT</button>
<button type="submit" class="<?php if( $pType == 'continuous' ){ echo 'active '; } else echo 'noClass '; ?>btn btn-primary waitingUiBut" id="but4" name="s" value="continuous"action="pumpCat">CONTINUOUS</button>
</div>
</form>
Any pointers would be greatly appreciated.
Your action="?pType=" seems to set the default value properly
But, there is no 'href' attribute in a button. Try using an element with proper href and use that to submit the form.
To fix this problem I simply added a query string on the navbar link
<li class=" animateNav <?php if ($pageCat == "Well Service"){ echo "active"; } ?>"><a id="link" href="wellservice.php?s=intermittent">WELL SERVICE</a></li>
and also amended the forms using GET to change the query parameters on the buttons onclick event
<form name="pumpType" action="?s=<?php echo $pType ?>" method="get" align="center">
<div class="btn-group" data-toggle="buttons-radio">
<button type="submit" class="<?php if( $pType == 'intermittent' ){ echo 'active '; } ?>btn btn-primary waitingUiBut" id="but1" name="s" value="intermittent" action="pumpCat">INTERMITTENT</button>
<button type="submit" class="<?php if( $pType == 'continuous' ){ echo 'active '; } ?>btn btn-primary waitingUiBut" id="but4" name="s" value="continuous"action="pumpCat">CONTINUOUS</button>
</div>
</form>
If I understand your goals correctly, I would rethink your apps structure.
You mention you want to use AJAX, so I'd have my SQL query in a separate PHP file, then post the click or slider function value to that file using jQuery, something like:
jQuery
Then update your main HTML file with the returned values.
There is a basic example:
PHP MySQL AJAX

Categories