For this code:

define('WP_USE_THEMES', false);

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";

This call throws no errors, warning or notices, and shows the message Done.

https://www.example.com/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?

  • Besides i am getting the error include_once:322 mocd: !is_admin(), or doing cron -- exiting for both calls... – Brethlosze Jan 18 '19 at 15:18
  • 1
    Have you considered using WP CLI commands instead? It's a lot easier than rolling your own from scratch – Tom J Nowell Jan 18 '19 at 16:24
  • 1
    how to write a WP CLI command: make.wordpress.org/cli/handbook/commands-cookbook there's no need to bootstrap your own scripts when all the problems have already been solved by an official tool – Tom J Nowell Jan 18 '19 at 16:25
  • The WP CLI looks very promising. But at this point, I still need PHP for running some very simple operations (search post by slug) which WP CLI do not have implemented. Somebody wrote a plugin for fixing that. Some part is solved in the WP REST API, and the other part is solved in the WP CLI. Then i should mix both these inside... PHP. Not so promising though. – Brethlosze Jan 18 '19 at 16:33
  • @TomJNowell Yes... All this week i've been trying to make the WP REST API work, and yesterday the WP CLI, and both of them simply do not get the job done. WP REST API do not upload and attach media files and thumbnails, and WP CLI cannot find by slug. Hence i just made my own PHP code and it just works. – Brethlosze Jan 18 '19 at 16:37

You 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:

  • 1
    +1 The WP CLI wp post list documentation declares a post_name field... I don't know why the name field is used in here instead. But with this modification the GitHub Get Post By Slug ticket has a solution. – Brethlosze Jan 18 '19 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.