Display item based on date of post
-
Hello, I am hoping someone can help me. I’ve searched far and wide and I cannot seem to find a solution, nor am I good enough with php to figure it out on my own.
I am trying to display a link on a post page, but only for 10 minutes after the post is made. This link will lead to a front-end editor so that the post may be edited. But like previously mentioned, I only want said link to display for 10 minutes after the post is made. How do I do this?
I have added the “Edit Post” link to my single.php file and it’s wrapped within a div. Help!
-
Assuming the link output is within the Loop, something like this:
if ( (int) get_the_date('U', get_the_ID()) > time() - 600 && is_user_logged_in()) { echo '<a href="', site_url('/front_edit_post/?post_id='), get_the_ID(), '">Edit</a>'; }
This will suppress the edit link if the request happens more than 10 minutes (600 seconds) after the publish date/time OR if the user is not logged in. Of course, a page sitting on one’s browser could have the link indefinitely after expiration as long as it was requested within the allowed time. Additional enforcement of the time needs to come when, 1) The front end edit form is requested, and 2) when the edit form is submitted to update the post. The number 2 enforcement might reasonably include an additional grace period to allow the user time to effect an edit after the edit form is loaded.
If further restrictions are required, such as only users with a certain capability can edit posts, that should be incorporated as well. By default users need ‘edit_published_posts’ capability to do so. There are ways around this if this is not suitable.
Oh my goodness, thank you so very much!! I was able to utilize this perfectly. I didn’t delve into the additional grace period after an edit. If I couldn’t get to this point on my own, I certainly won’t be able to figure that part out. But what you’ve provided will definitely suffice.
Thank you again!!
Happy to help!
Sure, a grace period is certainly not mandatory. A consideration at most. The important point was just removing the link is insufficient protection. The best protection occurs when the edits are submitted. Links can be gotten around. Posts to the server cannot.Hello again @bcworkz. I was wondering if you might be able to assist me again.
In addition to the “Post Edit” link that you helped me with, I want to do the same with a delete option. However, I haven’t been able to get it to work properly. I think it may be an issue with my commenting, single vs double quotes, backslashes, etc. But I haven’t had any luck. I appreciate any help you can offer.
Here’s the code I’m trying to use:
<div class="post-delete"><span class="delete-post"> <?php if ( (int) get_the_date('U', get_the_ID()) > time() - 2800 && (is_user_logged_in() && $current_user->ID == $post->post_author)) { echo ' <p><a onclick="return confirm("Are you SURE you want to delete this post?");" href="<?php echo get_delete_post_link( $post->ID ) ?>">Delete post</a></p>'; }else{ ?> <style type="text/css">.post-delete{ display:none; }</style> <?php }?> </span></div>
While using the code above, the “delete post” link shows, but I get a 404 page because the link actually leads to: <?php echo get_delete_post_link( $post->ID ) ?> instead of the on-click function and delete action.
Yes, you are correct, a nested quotes issue. The innermost quotes need to be single, but then that would conflict with the outermost singles. We get around this by escaping the inner singles with a backslash. There’s also nested < ?php ? > blocks that cannot be. To include function returns, concatenate them into your string. Fix these problems like so:
echo ' <p><a onclick="return confirm(\'Are you SURE you want to delete this post?\');" href="' . get_delete_post_link( $post->ID ) . '">Delete post</a></p>'; //ignore this line, it's about how code is formatted in the forums
Thank you again so very much. I really appreciate your help!
You’re welcome!
Hello again. ??
It appears that I have found myself in another rut.
Thanks to your help, I’ve got the code to work perfectly on my development site. I just moved things over to my live site and my code just isn’t working. I’ve found that it I remove the time checks then it works…but the time check is important. These are the codes I’m using in my single.php file:
<div class="post-edit"><span class="edit-post"> <?php //allows author to edit post up to 10 minutes after it is published if ( (int) get_the_date('U', get_the_ID()) > time() - 600 && (is_user_logged_in() && $post->post_author == $current_user->ID)) { echo '<a href="https://www.website.com/posts">Edit Post</a>'; }else{ ?> <style type="text/css">.post-edit{ display:none; }</style> <?php } ?></span></div> <div class="post-delete"><span class="delete-post"> <?php // allows author to delete post up to 5 minutes after it is published if ( (int) get_the_date('U', get_the_ID()) > time() - 300 && (is_user_logged_in() && $current_user->ID == $post->post_author)) { echo '<a onclick="return confirm(\'Are you SURE you want to delete this post?\');" href="' . get_delete_post_link( $post->ID ) . '">Delete post</a>'; }else{ ?> <style type="text/css">.post-delete{ display:none; }</style> <?php }?> </span></div>
Can you think of any reason at all that I would have an issue with this? I checked the user capabilities, and everything looks good. But once a post is made, by default it’s just hidden from the “else” function. I’m at a loss and although I searched a lot online locating someone with a similar issue is very hard to find.
My guess is there is a discrepancy between the WP time keeping as used in get_the_date() and the server’s time keeping used in time(). Try using current_time(‘timestamp’) in place of time() so both functions are working off the same assumptions.
Be prepared for continued confusion over l10n time and time zones. The deeper I’ve dug, the more confused I’ve become! In many ways it’d be so much easier if the entire world used UTC and simply adjusted what times they do things ?? Does it really matter if we ate breakfast at 22:00 as long as sun up recently occurred (or is about to for early risers)? See now, you’ve gotten me started on one of my peeves!
Oh my gosh. It worked!!
Thank you so much once again. I wouldn’t have thought to do that. The strange thing is that both sites are the same server. My dev site is just built into a subdomain. Maybe that makes things a bit wonky.
I’m on my way! I really think that was the last of my hangups with my site. Thanks for being such a great help.
Oh my word, I spoke too soon!
So with the current_time(‘timestamp’) it displays, but it does not hide it after the time limit is reached. Grrrr….
EDIT: I got it. It was the comments again. Edited the single quote and now it’s good!! YAY!!
-
This reply was modified 7 years, 10 months ago by
shemakeswebsites.
You’re welcome!
That was a happy accident. I’d assumed you developed on a different server, where that sort of thing is common. Being on the same server, the discrepancy must be within the WP installations. I’m guessing your dev site’s timezone setting (General Settings) matches the server’s, but the production site is different.
Not that it matters much, as long as the problem is solved. I always have an innate urge to try and explain mysteries.
-
This reply was modified 7 years, 10 months ago by
- The topic ‘Display item based on date of post’ is closed to new replies.