Delete post from front end of wordpress - wordpress

Here's the code I have:
<?php
global $wp_query;
$postid = $wp_query->post->ID;
$url = get_bloginfo('url');
if (current_user_can('edit_post', $post->ID)){
echo '<a href="';
echo wp_nonce_url("$url/wp-admin/post.php?post=$postid&action=delete", 'delete-post_' . $post->ID);
echo '">Delete your listing</a>';
}
?>
I'm trying to delete the post outside the wordpress loop.
When I click the link, I get:
"Your attempt to delete this post: “post-name” has failed.
Please try again."
Does anyone know why that would be?

Looking at the reference, it looks like current_user_can() only takes one argument.
Perhaps the root cause here is a permissions issue for the user. You should be checking if a user has the delete_posts capability as seen below. Note that delete_posts checks if the user can delete their own posts. To check if the user can delete the posts of other users, you can use delete_other_posts instead.
if (current_user_can('delete_posts')){
echo '<a href="';
echo wp_nonce_url("$url/wp-admin/post.php?post=$postid&action=delete", 'delete-post_' . $post->ID);
echo '">Delete your listing</a>';
}
(Assuming you are using WP version 2.1 or later)

Related

WordPress: page Template Conditional checking not working in functions.php

I want to add a stylesheet in WordPress only if a user is using a specific template but conditional page template ( is_page_template('templa_name) ) is not working and code is falling into the else part. I have even gotten the right name of the template through code and tried adding that code but it didn't work. I can easily add and continue my development but it is strange and would like to know the solution to this weird problem.
Tried the code inside the template file and stackoverflow. there is an anwer but to no avail. Tried different variets of the code but it goes the same way
if ( is_page_template('page-templates/page-landing-page.php') ) {
function this_echoes() {
$looking_at = is_page_template( 'page-templates/page-landing-page.php' );
echo $looking_at;
echo '<br>';
echo 'if';
echo '<br>';
$page_template22 = get_page_template_slug( get_queried_object_id() );
echo $page_template22;
}
} else {
function this_echoes() {
$looking_at = is_page_template( 'page-templates/page-landing-page.php' );
echo '<br>';
echo $looking_at;
echo '<br>';
echo '$looking_at';
echo '<br>';
$page_template22 = get_page_template_slug( get_queried_object_id() );
echo $page_template22 ;
echo 'else';
}
}
I want the code to fall under the if statement rather then else statement. I do know that I am checking the right way.
Your if condition needs to go in the main this_echoes function.
I would suggest using template name slugs
(https://codex.wordpress.org/Function_Reference/get_page_template_slug) and then using an if check get your job done

How to print current_user name and Logout button in wordpress

I want to display this
Current user name - Logout( redirect to home page )
This is my current code:
echo $current_user->display_name . "\n";
echo Logout
But when i click on a link, i get only "" in address bar. The function is not being recognized.
Thanks
Try
echo 'Logout';
you are mixing php code and echo
This will give you the current username of logged in admin.
global $current_user;
echo $current_user->user_login;
Function Refrence
You need to include the global variable and initialize it for it to be usable.
Above where you need to call it, add this
global $current_user;
get_currentuserinfo();
echo $current_user->display_name
It will call in the variable and allow access to its data and functions.
Read more about getting use information HERE
For the URL, try this:
echo '<a href="'. wp_logout_url(home_url()) .'" title="Logout" />Logout</a>';

Wordpress Primary Category

I recently needed to change my permalink structure in Wordpress. However the change caused all my social sharing counters to reset. I found this great script which I modified below, http://encosia.com/preserving-social-sharing-counters-through-a-url-change/. I rewrote it so it would remove the %poste-id% at the end of the url on older posts.
The problem is that for the most part this works, however I need it to call the permalink category not the first category in the get_the_category(); array. Is there anyway I can pull this info, or modify this script to work that way? This is also being used in the loop. Thanks!!
<?php
$url_change_id = 68135;
$postid = $post->ID;
$category = get_the_category();
$slug = $post->post_name;
$permalink_url = get_permalink();
if (intval($postid) < $url_change_id) {
$url_date_prefix = "/" . $category[0]->category_nicename .
"/" . $slug .
".html";
$sharing_url = str_replace($permalink_url,
"http://website.com" . $url_date_prefix,
$permalink_url);
} else {
$sharing_url = get_permalink();
}
?>
<?php echo $sharing_url; ?>

how to determine is_home in wordpress if query a category?

I need to query a category in home page. in index.php file I used this script
$all_featured_posts = query_posts(array('category_name'=>'featured-programs'));
Then in the header.php file I need to change the title
<title>
<?php
if ( is_home() ) {
echo 'My site name' ;
} elseif (is_404()) {
echo '404 Not Found';
} elseif (is_category()) {
echo ' Category' . wp_title('',0).' | My site name' ;
}
?>
The problem is when I query a category in the index file then the is_home return false ( Tried with is_front_page() also ) Then it alway show the title with the name of the category which I query.
How I can fix it? Thanks you!
I might be wrong, but I think because you use query_posts(), all your is_* functions change their values. And, well, because you do query a category, is_home() should return false.
What you can do to solve it, is use new WP_Query(), and get all the posts from it. This way, you will not be affecting the original WP_Query, and thus the is_* functions.
The code should look like this:
$query = new WP_Query('category_name=featured-programs');
while ( $query->have_posts() ) : $query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();

Can't get my WordPress site to update its own feed?

I'm using the following code to display some entries of my WordPress blog as a feed in the sidebar. The problem is, it's not updating no matter what I do. It still only shows the first "Hello World" post, even though I've added others, and it doesn't even show the updated name of that post after I've changed it. Thought this might be a caching issue, but if I actually click into the feed XML, the data is updated- which makes no sense to me??
<?php
// Blog Feed:
$rss_url = get_option('home')."/feed/";
?>
<ul class="side-feed">
<?php if(function_exists('fetch_feed')) {
include_once(ABSPATH . WPINC . '/feed.php'); // include the required file
$feed = fetch_feed($rss_url); // specify the source feed
$limit = $feed->get_item_quantity(3); // specify number of items
$items = $feed->get_items(0, $limit); // create an array of items
echo count($items);
}
if ($limit == 0) echo '<div>(None)</div>';
else foreach ($items as $item) : ?>
<li><?php echo $item->get_title(); ?></li>
<?php endforeach; ?>
</ul>
Wordpress caches feeds for 12 hours by default, to change this you need to hook into the wp_feed_cache_transient_lifetime filter and return the number of seconds you want to cache for.
add_filter('wp_feed_cache_transient_lifetime', create_function('', 'return 60*60;'));

Resources