Wordpress : How to hide page depend on time in php - php

I'm looking to hide some page of a website between 10 AM AND 20 PM each day
I add the code below on the file function.php
<?php
if (date('H')>10 && date('H')<20{echo 'url page';}
?>
This code breaks the website. I'm looking for the correct code to add.

Besides syntax errors mentioned in comments, I guess you need to check whether it is the particular page you need to lock down, so you won't do that to the entire site. If that page is some WordPress post, you can block access by post ID, like this:
function blockAccessFromPost($post) {
$post_id = $post->ID;
$hours = intval(date('H'));
if ($hours >= 9 && $hours < 20 && $post_id === 1123) {
echo 'Please wait...';
exit; // exit is required so no further code is executed.
}
}
add_action('the_post', 'blockAccessFromPost');
In case if you don't know where to find the post/page id, here's the article.
The previous version of the answer was wrong, because it was unable to get access to the post data right away, WordPress was not initalized. the_post hook helps to execute the code in the moment when current post data has just loaded.

Still the same, I really don't know with, your code look perfect. When I use it, the post disappear in the list and I can see wirtten "Please wait...1391". But widget footer etc disappear.
I post you back the code below to be sure to use it well..
function blockAccessFromPost($post) {
$post_id = $post->ID;
$hours = intval(date('H'));
if ($hours >= 9 && $hours < 20 && $post_id === 1391) {
echo 'Please wait...';
echo $post->ID;
exit; // exit is required so no further code is executed.
}
return $post;}
add_action('the_post', 'blockAccessFromPost');
?>```

Related

Insert php snippet after checking correct password

I have a Wordpress page where a user can book language classes. There are different booking options, so I built the page in a way that the booking form charges dinamically from URL. The DOM charges 16 different shortcodes but in front-end it's only displayed the one that user enters in the URL by setting some parameters. The only problem about this is that the page takes a lot of time to charge and display the form.
I'm trying to figure out a way to charge only one shortcode and only set dinamically the value that differs between all shortcodes. After some researching (as my PHP knowledge is limited), I tried this code, and it worked:
function php_insert() {
if( is_page( 645 ) ) {
$awQueryString = $_SERVER['QUERY_STRING'];
parse_str($awQueryString, $awQueryStringParams);
$language = $awQueryStringParams['lang'];
$pack = $awQueryStringParams['pack'];
if (empty($awQueryString)) {
echo 'The URL entered is invalid. Please refresh the page with the correct URL or contact the web admin.';
}
else if ($language == 'english') {
if ($pack == '5') {
echo do_shortcode('[ameliacatalog package=13]');
}
if ($pack == '10') {
echo do_shortcode('[ameliacatalog package=14]');
}
if ($pack == '25') {
echo do_shortcode('[ameliacatalog package=15]');
}
}
}
}
add_action('wp_enqueue_scripts', 'php_insert');
The problem now is that this page is protected by password, and this snippet charges the code at the top of the page and it doesn't wait to check that the password is correct.
I don't know if this is the best way to do this workaround. Also don't know how can I do to solve the password check and location problem.
I hope I have explained everything well.
Thanks

how to display status online/offline of commentator in wordpress

i want to show the status of commentator in comment page of wordpress
i am trying to display simple message [online/offline] according to its status.
right now i am using below code
<?php
$user_id = get_comment(get_comment_ID())->user_id;
if ( is_user_logged_in($user_id) )
{
echo 'Online';
} else {
echo 'Offline';
}
?>
but the above code is not working. it only show text (Online) and not working properly. i think there is some error in above code can anybody correct it for me.

Update_Comment_meta is not defined

I am working with WordPress plugin, and I am doing following.
Create a new button in comments.
Click here
Now on magic.php I am calling update_comment_meta.
$decode_str=base64_decode($_GET['en']);
if(substr($decode_str, 0,4) != "http"){
$decode_str = "http://".$decode_str;
}
if(is_numeric($_GET['cid']) && is_numeric($_GET['pid']) ){
update_comment_meta( $_GET['pid'] , 'pinned', $_GET['cid'] );
}
header("Location: ".$decode_str);
?>
It givens me error, update_comment_meta is not defined.
What I think is I might need to load wp-load.php, because magic.php all alone doesn't know it is a part of wordpress.
I don't want to do AJAX. I have also read loading wp-load.php within your file is not a good idea.
Thanks.

need to refresh page to remove sessions

In this code I'm trying to ban client if he/she/it doing to much(10) login request for 3 minutes. The problem is after 3 minutes user must refresh the page 2 times. I can see the reason why it's enter into if statement but I can't find the solution. I feel like I've overcoded.
if($this->sessions->get_data("wrong_login")>10){
if(!isset($_SESSION["ban_time"])){
$this->sessions->set_data("ban_time", time());
}else
{
if(time() - $this->sessions->get_data("ban_time") > 180){ // 180 seconds
$this->sessions->remove("ban_time");
$this->sessions->remove("wrong_login");
}
}
// The message if user still banned
die("Banned for 3 minutes!");
}
I hope I can tell the problem..
EDIT: This code is the inside of the construct of register controller.
Before your IF statement, add another if statement that checks for ban_time session if the time is up, then set the wrong_login session to 0 if it is.
if($this->sessions->get_data("ban_time") < time())
{
$this->sessions->remove("ban_time");
$this->sessions->set_data("wrong_login", 0);
}
remove your else statement there.
also forgot to mention! when you set the ban time, it should be time() + 180.
if(!isset($_SESSION["ban_time"])){
$this->sessions->set_data("ban_time", time()+180);
}
use header function.
e.g.
header("Location: /path/to/some/file.php?error=Banned for 3 minutes.");
Then on the file.php you can do this:
<?php
// Parse error
$error = isset($_GET['error']) ? $_GET['error'] : '';
// Display error (if any) and stop executing the rest of the code.
if (!empty($error)) {
exit($error);
}
?>
This will not work if you already started to output...

Wordpress Delay content being published to non-members

I have a wordpress site that uses the WPMU Membership plugin. I want to add code to my template that displays a new post to members 5 days before it is shown to non-members.
Something like this:
if (current_user_on_level(17) && CODE TO DETERMINE IF POST IS LESS THAN 5 DAYS OLD) {
echo POST CONTENT;
}
else {
echo 'Info not available yet';
}
;
What code would I use to determine whether the published post is less than 5 days old??
I figured out the answer. First had to convert the post time to a UNIX code using this code:
(True sets it to UNIX epoch time rather than set to local time zone)
Then I used this code to get the UNIX time 5 days ago
strtotime("-5 day")
Put the two together with an if statement that displays the content or if it's not time it displays a message that the content isn't available yet.
<?php
$post_timestamp = get_post_time('U', true);
if (current_user_on_level(17) && $post_timestamp <= strtotime("-1 day"))
{
get_template_part( 'content', get_post_format() );
}
else {
echo 'No post available yet <br />';
}?>
The only issue with this is that because my posts are ordered in chronological order, the top of my blog is filled with the line "No post available yet". Can anyone think of how I can get these statements to sit at the bottom?

Categories