For this code:
<?php
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
if (PHP_SAPI === 'cli') {
$post_id =$argv[1];
$post_title =$argv[2];
}
else {
$post_id =$_GET['post_id'];
$post_title =$_GET['post_title'];
}
if (empty($_GET)){
$post_id = 0;
$post_title='My Post Title';
}
echo "Done";
return;
This call throws no errors, warning or notices, and shows the message Done
.
.php?post_id=0&post_title=My Post Title
This call
php -f ~/public_html/function.php -- 0 'My Post Title'
throws the following warning and notice, and do not show the message Done
:
Notice: Undefined index: SERVER_PROTOCOL in /home/examplecom/public_html/wp-includes/load.php on line 16
Warning: Cannot modify header information - headers already sent by (output started at /home/examplecom/public_html/wp-includes/load.php:16) in /home/examplecom/public_html/wp-includes/pluggable.php on line 1223
How should I fix that?
For this code:
<?php
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
if (PHP_SAPI === 'cli') {
$post_id =$argv[1];
$post_title =$argv[2];
}
else {
$post_id =$_GET['post_id'];
$post_title =$_GET['post_title'];
}
if (empty($_GET)){
$post_id = 0;
$post_title='My Post Title';
}
echo "Done";
return;
This call throws no errors, warning or notices, and shows the message Done
.
https://www.example/function.php?post_id=0&post_title=My Post Title
This call
php -f ~/public_html/function.php -- 0 'My Post Title'
throws the following warning and notice, and do not show the message Done
:
Notice: Undefined index: SERVER_PROTOCOL in /home/examplecom/public_html/wp-includes/load.php on line 16
Warning: Cannot modify header information - headers already sent by (output started at /home/examplecom/public_html/wp-includes/load.php:16) in /home/examplecom/public_html/wp-includes/pluggable.php on line 1223
How should I fix that?
Share Improve this question edited Jan 18, 2019 at 15:35 Brethlosze asked Jan 18, 2019 at 15:16 BrethloszeBrethlosze 1491 gold badge2 silver badges11 bronze badges 10 | Show 5 more comments1 Answer
Reset to default 1You can do this:
function foo_command( $args ) {
WP_CLI::success( $args[0] );
}
WP_CLI::add_command( 'foo', 'foo_command' );
And put it in a plugin, then run:
wp foo "hello world"
And it will print out hello world.
Just because the built in subcommands don't do what you need them to do, doesn't mean you can't add your own. WP CLI will take care of bootstrapping WordPress and loading things properly ( and it avoids the security issues of standalone endpoint PHP files such as the one in your question )
However, I suspect the solution to your actual problem is this:
wp post list --name="hello-world" --fields="id"
And for the REST API:
https://example/wp-json/wp/v2/posts/slug?="hello-world"
include_once:322 mocd: !is_admin(), or doing cron -- exiting
for both calls... – Brethlosze Commented Jan 18, 2019 at 15:18