admin管理员组文章数量:1316833
I have a standalone PHP script in my WordPress theme's directory that I run once every hour through a cron job (or manually if needed). All other WordPress functions are working except the update_option()
function.
A simplified version of my script looks like this:
require_once('/path/to/site/wp-load.php');
$value = my_function();
update_option('my_option', $value);
and in one of my theme's files, I am running the following code:
echo get_option('my_option');
Nothing is printed, and a var_dump
shows that the returned value is false
.
My wp-admin/options.php
page doesn't list my_option
either.
I'm at a loss, because below those lines, I am using the following WordPress functions to interact with my WordPress database successfully:
- get_posts
- delete_post_meta
- add_post_meta
Debugging my script, my_function
returns a string (about 10 characters) and no errors are thrown with my PHP error settings at E_ALL
.
Do I need to include other WordPress core files? I thought that wp-load.php
was all you needed.
WordPress version: 3.7
I have a standalone PHP script in my WordPress theme's directory that I run once every hour through a cron job (or manually if needed). All other WordPress functions are working except the update_option()
function.
A simplified version of my script looks like this:
require_once('/path/to/site/wp-load.php');
$value = my_function();
update_option('my_option', $value);
and in one of my theme's files, I am running the following code:
echo get_option('my_option');
Nothing is printed, and a var_dump
shows that the returned value is false
.
My wp-admin/options.php
page doesn't list my_option
either.
I'm at a loss, because below those lines, I am using the following WordPress functions to interact with my WordPress database successfully:
- get_posts
- delete_post_meta
- add_post_meta
Debugging my script, my_function
returns a string (about 10 characters) and no errors are thrown with my PHP error settings at E_ALL
.
Do I need to include other WordPress core files? I thought that wp-load.php
was all you needed.
WordPress version: 3.7
Share Improve this question edited Oct 29, 2013 at 4:09 iglvzx asked Oct 29, 2013 at 3:57 iglvzxiglvzx 2463 silver badges6 bronze badges 3 |4 Answers
Reset to default 2I'm not sure why it does't work for you, but the following works in the file wp-content/test.php
:
<?php
// doesn't make difference to have this or not, for the rest to work
define( 'WP_USE_THEMES', false );
require( $_SERVER['DOCUMENT_ROOT'] .'/wp-load.php' );
function my_function()
{
return 'hello world';
}
$value = my_function();
update_option( 'my_option', $value );
var_dump( get_option( 'my_option' ) );
Add this two line in your file top
$parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
require_once( $parse_uri[0] . 'wp-load.php' );
It loads all of WordPress, but doesn't call wp() or invoke the template loader (used by themes) .
It sounds you need some kinda of bootstrap for your code. WordPress has already bootstrap such as index.php
or wp-load.php
or even wp-blog-header.php
.
This :
require_once('/path/to/site/wp-load.php');
is not recommanded because path can be modified. What you can do to have some bootstrap is :
// Load WP
$load = 'wp-load.php';
while( !is_file( $load ) ) {
if( is_dir( '..' ) )
chdir( '..' );
else
die( 'Could not find WordPress in this place!');
}
require_once( $load );
In this way you'll be able to load WordPress almost in any case.
This is very old question but I will still comment on it. Now you have multiple options: WP-CLI should work in almost all of the cases. Another option is to create a custom plugin that hooks into 'init' event and it does something.
本文标签: optionsupdateoption not working in standalone PHP script
版权声明:本文标题:options - update_option not working in stand-alone PHP script 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742014110a2413443.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wp-admin/options.php
page doesn't showmy_option
either.function_exists('update_option')
returns true. – iglvzx Commented Oct 29, 2013 at 4:09