最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Passing PHP Function Arguments from CLI vs URL

matteradmin9PV0评论

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
  • Besides i am getting the error include_once:322 mocd: !is_admin(), or doing cron -- exiting for both calls... – Brethlosze Commented Jan 18, 2019 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 Commented Jan 18, 2019 at 16:24
  • 1 how to write a WP CLI command: make.wordpress/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 Commented Jan 18, 2019 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 Commented Jan 18, 2019 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 Commented Jan 18, 2019 at 16:37
 |  Show 5 more comments

1 Answer 1

Reset to default 1

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:

https://example/wp-json/wp/v2/posts/slug?="hello-world"

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far