Update_Comment_meta is not defined - php

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.

Related

Wordpress : How to hide page depend on time in 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');
?>```

How to remove the included file in PHP

I"m loading a file in start of PHP. I have declared some activity and assign them to buttons which are aligned left and right side of HTML document. On click of each button will included some Other PHP file but I want to remove the previous loaded on startup file before including any other file.
Here is my code
<?php
// php file on startup of file
include("dashboard.php");
$activity = $_REQUEST['activity'];
if($activity) {
if($activity == 'addMember'){
include("addMember.php");
remove("dashboard.php");
}
if($activity == 'dashboard'){
}
if($activity == 'issueBooks'){
include("issueBooks.php");
}
if($activity == 'returnBooks'){
include("returnBooks.php");
}
<?php
I tried with
if($activity == 'addMember'){
include("addMember.php");
remove("dashboard.php");
}
But since I"m very new to PHP, it didn't work as expected.
Any Help anyone.
That isn't how include works. When you include a file, the contents of that file are executed at that point in the code. There's no straightforward way to undo that.
It looks like you want the dashboard activity to be the default view if no activity has been selected yet. To do that, you can just include it only when there is no activity value in request, or when the dashboard activity has been specifically requested.
if (empty($_REQUEST['activity']) || $_REQUEST['activity'] == 'dashboard') {
include 'dashboard.php';
}

Drupal JS on content type

Hi I content type 'site_location', /js/site_location.js, and this is on Drupal 7:
I want to run the JS file only on the 'site_location' nodes, but it also runs when I go to the edit page too. Is there any way of not running the javascript when I edit a node of content type 'site_location'?
Here is what I have:
function porto_preprocess_page(&$vars, $hook) {
if($vars['node']->type === "site_location"){
drupal_add_js($theme_path . '/js/site_location.js' , 'file');
}
}
Just messing around I found a solution.
arg(2) is 'edit' on the edit page and empty on the view page. Therefore I ended up with:
if($vars['node']->type === "site_location" && arg(2) == ''){
I still wonder though if there are other approaches to this.
Something like this should do the trick:
if ($vars['node']->type === "site_location" && path_is_admin(current_path())) {
drupal_add_js($theme_path . '/js/site_location.js' , 'file');
}

Checking multiple $_ POST words with PHP

I'm working with a page that, once a link is called this script checks and if the POST contains the keyword it and then finds that page. However no matter how I organize this if it doesn't work.
<?PHP
if($_POST['page']) {
$page = (int)$_POST['page'];
$exists = file_exists('pages/page_'.$page.'html');
if($exists) {
echo file_get_contexnts('pages/page_'.$page.'html');
} else {
echo 'There is no such page!';
}
} else if ($_POST['course']) die("0"); {
$course = (int)$_POST['course'];
$exists = file_exists('courses/course_'.$course.'html');
if($exists) {
echo file_get_contexnts('courses/course_'.$course.'html');
die("1");
} else {
echo 'There is no such page!';
}
}
?>
The error I'm currently receiving with this setup is:
Notice: Undefined index: course in C:\wamp\www\Home Page\load_page.php on line 12
Call Stack
# Time Memory Function Location
1 0.0003 253944 {main}( ) ..\load_page.php:0
Is it because there is no 'course' in the page? I might be confused of the code I'm modifying a tutorial of a simple ajax website. It is possible what I am trying to do does not work.
In that case how could I possible go about doing what I want to do.
Right now I have a home page and it loads in another page without switching pages. I like the floridness of it. I would like to have a sort of sub call. So if you are on the home page and you go to courses page then you can click on a specific course and that will load from a different directory within the courses directory.
Homepage (when you click on courses you go to...)
pages/courses_home.html (when you click on a course you go to...)
courses/course_1.html (you can view course and then click back to directory above or go to home)
That is the structure I'm looking to try to achieve.
If more information is needed please let me know what and I'll do my best to include it. Thank you.
The syntax should be:
if(isset($_POST["page"])) {
} elseif(isset($_POST["course"])) {
}
I am not sure why you have a die statement there, but I don't think it belongs. Also, keep in mind the logic for what happens if neither of these conditions is met.
Edit: also keep in mind that isset doesn't prevent empty strings, so you may want to check for that as well. A function you could use is
function checkPost($value) {
return isset($_POST[$value]) && $_POST[$value] !== "";
}
To use:
if(checkPost('page')) {
//some logic
}
Wrong syntax.
elseif ($_POST['course']) {
without die statement.If 'course' undefined else statement works and does not get error. Sorry for bad English.
Try this:
if isset(($_POST['page'])) {
...
} else if isset(($_POST['course'])) die("0"); {
instead of this:
if($_POST['page']) {
...
} else if ($_POST['course']) die("0"); {

Wordpress if_logged_in function on Non-wordpress page

I want to somehow use this function on a non-wordpress page :
<?php
if (is_user_logged_in()){
echo "Welcome, registered user!";
}
else {
echo "Welcome, visitor!";
};
?>
Is there any way to do this? I was told it is possible if I include my wp-header.php in the Non-wordpress page, but I do not want to do this. Any other methods?
You have to load the Wordpress Core into your current script. You can do it like this:
//wp-admin
require $_SERVER['DOCUMENT_ROOT'].'/wp-load.php';
//Check if someone is logged in
global $user_level;
get_currentuserinfo() ;
//echo "User Level = $user_level<br>";
if ($user_level < 1)
die('You aren\'t allowed to view this!');
You have to use the Wordpress header because that function is in the core. The only other option is to write your own function with the same name, using the same database of users.
You don't need to include the admin-header.php file. Just include this file:
wp-includes/pluggable.php
This file contains definition of is_current_logged_in() as well as many other user data helpers such as wp_set_current_user(), wp_get_current_user() etc.
Including wp-load.php is the best way to go. There are loads of ways around this, but I use something similar to the following:
$wp_load_location_array = array();
$wp_load_location_array[] = "../../../../../../wp-load.php";
$wp_load_location_array[] = "../../../../../wp-load.php";
$wp_load_location_array[] = "../../../../wp-load.php";
$wp_load_location_array[] = "../../../wp-load.php";
$wp_load_location_array[] = "../../wp-load.php";
foreach($wp_load_location_array as $wp_load_location)
{
if(file_exists($wp_load_location))
{
require_once($wp_load_location);
}
}
It's a bit of a hack and if you actually know the location of the wp-load.php file, then you can just use that in the require_once() function.
Put that at the top of your file and you'll be able to use all of WordPress' functions in your non-wordpress page.

Categories