WordPress having trouble working outside of itself - php

I'm trying to call WordPress functions on an external page. I've been able to get rid of the first hurdle which had the error of:
This Plugin Requires WordPress 3.1+ or Greater: Activation Stopped!
By literally disabling everything on my site.
Now I am facing another little issue.
I've added this requirement statement: <?php require('/path/to/my/wp-blog-header.php'); ?>
And I get the following error. Of course this requirement statement is outside of any class.
Fatal error: Call to a member function main() on a non-object in /home2/phanime/public_html/wp-includes/functions.php on line 781
I've also looked around before asking, and people said to add the following line: global $wpdb; and it didn't help, the error still persists.
I've also tried simply putting that line of code (the required statement) and the issue still persists.
As suggested by someone, I'll update this question. I've got the initialization working but now I am not able to use WP_Query or get_posts and they don't return anything. For example this code I tried
$testingQuery = get_posts(array(
'post_type' => 'anime',
'order' => 'date'
));
Then with an if / else statement I check if the variable is empty or not
if ($testingQuery) {
foreach( $testingQuery as $post ) : setup_postdata($post);
$contents .= '<li id="ourTab" class="profileContent">
<div class="section">AnimeList Coming soon.</div>
</li>';
$contents .= get_the_title();
endforeach;
} else {
$contents .= '<li id="ourTab" class="profileContent">
<div class="section">Test</div>
</li>';
}
In this case the else statement runs which tells me that the variable is empty or what not, as "Test" is displayed.
Furthermore, I remove the if / else statement and just try the foreach loop to see the exact error I get.
And this is the error
Invalid argument supplied for foreach() in /home2/asdf/public_html/community/library/asdfTab/Listener.php, line 54:
I've also tried the same thing with WP_Query and the variables are returning empty so I don't know what the exact problem is, but it seems that they are not able to retrieve the posts that I am requesting.

you need to initialize WordPress before calling any functions.
Try requiring wp-config.php into your external page before calling any functions.

put the following in the beginning of your external script
<?php
require_once("path/to/wp-load.php");
then wp will bootstrap and you can call additional functions

Looks like you need to reinstall Wordpress. BACKUP YOUR STUFF BEFORE DOING IT.

Related

Having trouble getting $_GET variables in Wordpress shortcodes

Calling $_GET['ip'] worked up until a recent wordpress update and now it's broken.
I don't know how Wordpress expects me to get the variable but the code I've been messing with and put together doesn't seem to work at all.
I'm clearly doing something wrong but I can't seem to wrap my head around making this work.
The code I'm trying to work with is here: https://pastebin.com/4iipisjU
UPDATE: The code works, the WPSupercache configuration file for nginx is what seems to have broken it.
You should be able to use get_query_var
<?php
$value = get_query_var( "paramA", "default value" );
?>
Also, $_GET['ip'] will refer to the query parameter IP that was passed as part of the request.
Is this actually what you are looking for? Or are you trying to see the IP of the client making the request? If the latter, this is incorrect.
Try change $get_ip_addr to:
$get_ip_addr = get_query_var('ip', $_GET['ip']);

Drupal 7 include php file error

l am working on a Drupal 7 site with a custom video block called a leaf. It allows the admin the ability to add Text, Photos and Video. When I add video, it functions properly, but I get the below error.
Strict warning: Only variables should be passed by reference in include() (line 27 of /var/www/vhosts/xxxxxxxxxxx.com/sites/default/themes/custom_theme/templates/views-view-field--leaf-block--nid.tpl.php)
I researched the error and came across similar instances, but the php is a bit over my head.
The code currently reads as:
<?php endif; ?>
<?php
$node_to_load = node_load($row->nid);
//line 27 below
print drupal_render(node_view($node_to_load));
?>
I believe that render needs a reference so as is it is invalid, but I am not sure how to accurately assign the return value or the correct syntax. My attempt is below, but is incorrect.
<?php
$node_to_load = node_load($row->nid);
{
nid++;
}
$reference=$something
node_to_load($reference);
// $something to reference????
?>
Does anyone have any suggestions or advise that would help me? This error doesn’t appear to interrupt the functionality of the videos once added but I would love to clean up the code if possible and understand the situation more.
Thanks,
try this:
<?php
$node_to_load = node_load($row->nid);
$node_view = node_view($node_to_load);
print drupal_render($node_view);
?>
As you can see in the docs, drupal_render uses a reference.

PHP code for a total newbie with just enough knowledge to debug

I'm just logical, not a coder.
I'm also not english, so i hope you will understand me.
I have some troubles with some PHP scripts and i don't find out where's the problem.
This code is working well as a standalone. When used with another module (joomla), it returns this error.
Code :
}
// Used for styling the active article
$item->active = $item->id == $active_article_id ? 'active' : '';
$item->title = self::truncate($item->title, $title_limit);
if ($show_introtext) {
$item->introtext = JHtml::_('content.prepare', $item->introtext, '', 'mod_sj_carousel.content');
self::getAImages($item, $params);
$item->introtext = self::_cleanText($item->introtext);
} else {
$item->introtext = JHtml::_('content.prepare', $item->introtext, '', 'mod_sj_carousel.content');
self::getAImages($item, $params);
}
$item->displayIntrotext = $show_introtext ? self::truncate($item->introtext, $introtext_limit) : '';
$item->displayReadmore = $item->alternative_readmore;
}
return $items;
}
}
Returned Error :
Fatal error: Call to undefined method SjCarouselHelper::getAImages() in /home/abecedai/public_html/advvpm/modules/mod_sj_carousel/core/helper.php on line 159
I've no idea how and where i should define this method. I guess the problem is in the code, not fair for my PHP configuration. What is strange, is that it's working well without another joomla plugin activated (Jcomments). When Jcomments is activated, it goes wrong and crash.
I've been parsing the code, and did not found any function or declaration with similar name. Ther is definatly a conflict.
Parsing the module code where i get this error, this is the only place where i can find getAImages().
PHP ver is 5.3.25
Please consider me as a total noob in coding. I can understand what it does, but i'm not able to code myslef as i never learned how to.
I don't see the sense of this line if it doesn't exists
self::getAImages($item, $params);
Delete and run it again.
Look for SjCarouselHelper. There must be 2 versions of that and one is probably overwriting the other. Keep the newer version, copy it to both locations if possible.
This is just a guess of course since I can't see the whole thing - but I'm almost certain this is what happens.

PHP Call to undefined function

I am trying to call a function from another function. I get an error:
Fatal error: Call to undefined function getInitialInformation()
in controller.php on line 24
controller.php file:
require_once("model/model.php");
function intake() {
$info = getInitialInformation($id); //line 24
}
model/model.php
function getInitialInformation($id) {
return $GLOBALS['em']->find('InitialInformation', $id);
}
Things already tried:
Verified that the require_once works, and the file exists in the specified location.
Verified that the function exists in the file.
I am not able to figure this out. Am I missing something here?
How to reproduce the error, and how to fix it:
Put this code in a file called p.php:
<?php
class yoyo{
function salt(){
}
function pepper(){
salt();
}
}
$y = new yoyo();
$y->pepper();
?>
Run it like this:
php p.php
We get error:
PHP Fatal error: Call to undefined function salt() in
/home/el/foo/p.php on line 6
Solution: use $this->salt(); instead of salt();
So do it like this instead:
<?php
class yoyo{
function salt(){
}
function pepper(){
$this->salt();
}
}
$y = new yoyo();
$y->pepper();
?>
If someone could post a link to why $this has to be used before PHP functions within classes, yeah, that would be great.
This was a developer mistake - a misplaced ending brace, which made the above function a nested function.
I see a lot of questions related to the undefined function error in SO. Let me note down this as an answer, in case someone else have the same issue with function scope.
Things I tried to troubleshoot first:
Searched for the php file with the function definition in it. Verified that the file exists.
Verified that the require (or include) statement for the above file exists in the page. Also, verified the absolute path in the require/include is correct.
Verified that the filename is spelled correctly in the require statement.
Echoed a word in the included file, to see if it has been properly included.
Defined a separate function at the end of file, and called it. It worked too.
It was difficult to trace the braces, since the functions were very long - problem with legacy systems. Further steps to troubleshoot were this:
I already defined a simple print function at the end of included file. I moved it to just above the "undefined function". That made it undefined too.
Identified this as some scope issue.
Used the Netbeans collapse (code fold) feature to check the function just above this one. So, the 1000 lines function above just collapsed along with this one, making this a nested function.
Once the problem identified, cut-pasted the function to the end of file, which solved the issue.
Many times the problem comes because php does not support short open tags in php.ini file, i.e:
<?
phpinfo();
?>
You must use:
<?php
phpinfo();
?>
Your function is probably in a different namespace than the one you're calling it from.
http://php.net/manual/en/language.namespaces.basics.php
I happened that problem on a virtual server, when everything worked correctly on other hosting.
After several modifications I realized that I include or require_one works on all calls except in a file.
The problem of this file was the code < ?php ? > At the beginning and end of the text.
It was a script that was only < ?, and in that version of apache that was running did not work
This is obviously not the case in this Q,
but since I got here following the same error message I though I would add what was wrong with my code and maybe it will help some one else:
I was porting code from JS to PHP and ended up having a class with some public method.
The code that was calling the class (being code that originated from JS) looked something like:
$myObject.method(...)
this is wrong because in PHP it should look like this:
$myObject->method(...)
and it also resulted with "PHP Call to undefined function".
change to use -> and the problem was solved.
Presently I am working on web services where my function is defined and it was throwing an error undefined function.I just added this in autoload.php in codeigniter
$autoload['helper'] = array('common','security','url');
common is the name of my controller.
Please check that you have <?PHP at the top of your code. If you forget it, this error will appear.

Wordpress Enqueue Js scripts

I am having trouble getting wp_enqueue functions to work. I've looked at all the documentation on it but am having trouble sifting through and finding out what is supposed to go where.
so far I understand that I am supposed to register and enqueue the files from the functions.php file of the theme I am creating. So that is exactly what I do. I create some PHP tags and drop it in the middle of them, at the bottom of the page. Save and Upload.
When I reload, it just returns a blank white screen, must be an error in the code or something.
Here is the function:
<?php
function add_scripts(){
wp_register_script('jquery', 'http://code.jquery.com/jquery-1.5.2.min.js');
wp_register_script('nivo', get_bloginfo('url').'/scripts/nivo.js');
wp_register_script('slimbox',get_bloginfo('url').'/scripts/slimbox2.js');
wp_register_script('picasa', get_bloginfo('url').'/scripts/jquery.EmbedPicasaGallery.js');
wp_register_script('pwi',get_bloginfo('url').'/jquery.pwi-min.js');
wp_register_script('swf', get_bloginfo('url').'/jquery.swfobject.1-1-1.min.js');
wp_register_script('simpletube',get_bloginfo('url').'/scripts/jquery.simpletube.js');
wp_register_script('jqvalidate', get_bloginfo('url').'/jquery.jqvalidate.js');
wp_enqueue_script('jquery');
wp_enqueue_script('nivo');
wp_enqueue_script('slimbox');
wp_enqueue_script('picasa');
wp_enqueue_script('pwi')
wp_enqueue_script('swf');
wp_enqueue_script('simpletube')
wp_enqueue_script('jqvalidate');
}
add_action('init','add_scripts');
?>
So is there some sort of problem with my syntax? I'm not that strong with PHP.
Any help is greatly appreciated. Thanks!
It's kind of hard to debug it without seeing the whole file but the fact you get a 'blank page' suggests there's definitely something larger than a syntax problem somewhere.
Do you definitely have correctly nested php tags? i.e.
<?php
some code
<?php
some more code
?>
some more code
?>
will give you problems.
Also, it's now common practice to leave the last ?> from the end of the file (it means you wont have any issues with having whitespace after the closing tags and they're not necessary)
On top of that, you've used wp_register_script('jquery'...) - WordPress already has jquery registered. If you wish to re-register it, you need to wp_deregister_script('jquery') first. I'd also only do that outside of the admin, so:
if(!is_admin()){wp_deregister_script('jquery'); <your wp_register_script stuff> }
If these things don't help, copy and paste your entire functions.php file (use pastebin.com and give us a link)
As an aside, you're using get_bloginfo('url') several times - which means you're running lots of unnecessary calls to the database. Stick it into a variable and save yourself a little overhead:
$my_url = get_bloginfo('wpurl');
wp_register_script('thing', $my_url.'/script/location/file.js');
Oh! One more thing, I don't think url is an allowed argument for get_bloginfo() I think you want wpurl
Codex page on get_bloginfo() function
Good luck!
Missing ; for the following two lines:
wp_enqueue_script('pwi')
wp_enqueue_script('simpletube')
Instead of your code I would use:
<?php
function add_scripts(){
wp_enqueue_script('jquery', 'http://code.jquery.com/jquery-1.5.2.min.js');
wp_enqueue_script('nivo', get_bloginfo('url').'/scripts/nivo.js');
wp_enqueue_script('slimbox',get_bloginfo('url').'/scripts/slimbox2.js');
wp_enqueue_script('picasa', get_bloginfo('url').'/scripts/jquery.EmbedPicasaGallery.js');
wp_enqueue_script('pwi',get_bloginfo('url').'/jquery.pwi-min.js');
wp_enqueue_script('swf', get_bloginfo('url').'/jquery.swfobject.1-1-1.min.js');
wp_enqueue_script('simpletube',get_bloginfo('url').'/scripts/jquery.simpletube.js');
wp_enqueue_script('jqvalidate', get_bloginfo('url').'/jquery.jqvalidate.js');
}
add_action('wp_enqueue_scripts', 'add_scripts');
So please notice I have removed "wp_register_script" as using that is totally unnecessary if you are going to call wp_enqueue immediately after register.
wp_register_script
Is used so that you can afterwards call it ANYWHERE else in code without including the path.
Also big change is that I'm not calling the function from
init
But I'm calling it from
wp_enqueue_scripts
Also please consider adding additional parameters to your wp_enqueue_script such as
wp_enqueue_script( string $handle, string $src = '', array $deps = array(), string|bool|null $ver = false, bool $in_footer = false )

Categories