In my wordpress theme, I created a file called update.php. I can access this URL by
http://mydomain.com/wp-content/themes/mytheme/update.php
Here is the code inside update.php
global $wpdb;
// do some more stuff here
$wpdb->update( 'twitter_followers', array('count' => $followers), array('id' => '1') );
echo 'done';
When I go to this page I get this error.
PHP Fatal error: Call to a member function update() on a non-object in /var/www/vhosts/mydomain.com/subdomains/mytheme/httpdocs/wp-content/themes/mytheme/update.php on line 34
Any ideas on how I can fix this?
Also note, I did created twitter_followers table in my database.
The issue is that you don't have $wpdb yet, since your update.php is not part of wordpress. When you go to update.php wordpress is never loaded, so you get the non-object error. Try adding this line to the top of your update.php file, obviously changing the path:
require_once("/path/to/wordpress/wp-load.php");
the path will probably look something like this in a normal installation:
require_once("../../../../wp-load.php");
Related
I'm building a Wordpress site and for some application, an external website needs to access one of my page destination.php by the URL adress. So, I place this page at the root of my website. This page destination.php needs to access a table from my database. I thought using $wpdb would be enough to access any table but this variable does not seem to be initialized as a get the following error
Uncaught Error: Call to a member function get_results() on null
Following various advice from this forum, I tried :
require_once('../wp-config.php');
Then I get this error
Warning : require_once([...]/wp-settings.php): failed to open stream: No such file or directory in [...]
As advised for security reasons, wp-config is not in the root folder and so wp-config and wp-settings are not at the same level. Thus wp-config does not seem to be able to find wp-settings. I don't know if (and how) I should modify wp-config to provide it with the right path
require_once('wp-load.php');
Then I have no error and my script stops running. Thus I don't know what it's going wrong with wp-load. It is in the same folder as my file destination.php.
A minimal example is as follows
<?php
//require_once('../wp-config.php');
require_once('wp-load.php');
echo 'loading complete';
global $wpdb;
$myrows = $wpdb->get_results( "SELECT * FROM my_table" );
?>
Any help would be much appreciated. I've been stuck there for many days...
I changed the theme and it doesn't work well,it gives the following error:
public_html/wp-content/themes/7/dt-the7/inc/shortcodes/includes/gap/function.php line 56
I found the problem and removed the function _check_active_widget(),but this error appeared for all of the folders so I changed function.php file. It worked well for sometime. But after an hour when I changed the settings of site in WordPress, all of the function files re-changed to the first mood,and they had function _check_active_widget() again. I remove them again and again but it keeps coming back.
My Question is:
Why doesn't it work well?
Why is the mood getting reset?
And finally, how can I fix these function files?
Try to surround the function in /home/fxijdcel/public_html/wp-content/themes/7/dt-the7/inc/shortcodes/includes/columns/functions.php on line 86 with
if( !function_exists ( '_check_active_widget' ) )
In this way you declare this function only if is not been yet declared
im still new to wordpress and not that good in coding much in php but i do know the basics. I have here my main code file for my plugin called hello-world.php and another php file called display-data.php. i kinda confused on how you display data by using a function to call it on another page which in mine called display-data.php here is my code for hello-world.php
function wpdisplay(){
global $wpdb;
global $result;
$result = $wpdb->get_results("
SELECT * FROM wp_options WHERE option_id = '262'
", ARRAY_A);
print_r($result);
}
register_activation_hook('activate_hello-world/display-data.php' ,'wpdisplay');
i have a button form in my main file that directs to display-data.php where it should display the data. i tried calling it by wpdisplay();
however it gives me an error like this.
Call to undefined function wpdisplay() in /Applications/MAMP/htdocs/wordpress/wp-content/plugins/hello-world/display-data.php
hope you could help me and explain if there is something that i am missing. thanks
Like error message says, you are calling a function that is not defined. To use a function that you have defined on a different file, you have to include that file.
When I place the following code at the bottom of my new_grid.phtml template file, it works fine, without any any errors. But when it is placed at the top of the file, I get a fatal error: "Call to member function getRelatedLinkCollection() on a non-object in C:\wamp\www\MYSITE\magento\app\design\frontend\MYSITE\default\template\catalog\product\widget\new\content\new_grid.phtml on line 32.
<?php
foreach ($_product->getRelatedLinkCollection() as $link) {
$dats= $link->getLinkedProductId();
}
?>
I would like to get this code working specifically at the top of the file, because I'd like to output generated HTML in a particular structure. Any idea why I'm getting this error and what I can do to correct it?
Thank you!
Can you post the contents of your new_grid.phtml file?
Otherwise, make sure the foreach loop is immediately after the line which contains $_product =
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.