web-dev-qa-db-ja.com

Wordpress管理者で投稿IDを取得する方法

Wordpressプラグインを開発していて、現在の投稿IDを取得する必要があります
投稿の書き込み/ページの書き込み編集画面(ループ外)。

また、JavaScriptファイルにデータを渡したいので、「admin_print_scripts」フックの前にそれを行う必要があります。

使用できません:

$id = $_GET['post'];

新しい投稿またはページを追加するときに、URLにこの変数が含まれていないためです。

これまで私はこれらのオプションを試しましたが、どれも機能しませんでした:

A)これは0のIDを返します

function myplugin_setup() {
    global $wp_query;
    $id = $wp_query->get_queried_object_id();
    var_dump($id);
}

add_action('admin_init', 'myplugin_setup' );  

B)これはnullのIDを返します

function myplugin_setup() {
    global $wp_query;
    $id = $wp_query->post->ID;
    var_dump($id);
}

add_action('admin_init', 'myplugin_setup' );

C)これもnullのIDを返します

function myplugin_setup() {
    global $post;
    $id = $post->ID;
    var_dump($id);
}

add_action('admin_init', 'myplugin_setup' );
17
Victor

WordPressクエリの後にグローバル$ postを呼び出すようにしてください。initまたはadmin_initにアクションを追加すると、クエリの準備ができていないため、グローバル$ postから取得できるものは何もありません。変数。

私のアドバイスは、このページからアクションリファレンスを確認することです: http://codex.wordpress.org/Plugin_API/Action_Reference そしてあなたに合ったものを選択してください。

たとえば、私はこれを行いました:

add_action( 'admin_head', 'check_page_template' );
function check_page_template() {
    global $post;
    if ( 'page-homepage.php' == get_post_meta( $post->ID, '_wp_page_template', true ) ) {
        // The current page has the foobar template assigned
        // do something

    }
}

そして、WP adminでページIDを取得できました

11
Ciprian Tepes

使用する:

global $post

関数の開始時に。次に、$ post-> IDにアクセスして、現在の投稿のIDを取得する必要があります。これは、新規および既存の投稿で機能します。

8
Caleb

問題は、admin_initフックを使用していることです。アクションリファレンスを調べると--- http://codex.wordpress.org/Plugin_API/Action_Reference -このフックは実際には投稿をクエリする前に呼び出されることがわかります。そのため、使用する変数はそうではありません。まだいっぱいです。

後のアクション(is_admin()チェックを含む)を使用するか、admin initフックを使用して後のフックにアクションを追加することができるため、これもadminでのみ使用されます。

2
Tomáš Kapler

'admin_init'アクションは、ユーザーが管理領域にアクセスすると、他のフックの前にトリガーされます。新しい投稿がIDを取得する前にトリガーされます。

新しい投稿IDを取得するには、「save_post」を使用できます。これは、投稿またはページが作成または更新されるたびにトリガーされるアクションです( http://codex.wordpress.org/Plugin_API/Action_Reference/save_post )。

まず、「admin_enqueue_scripts」を使用してスクリプトを含め、次に「save_post」を使用して新しい投稿IDを取得できます。 'admin_print_scripts'は 'save_post'の後にトリガーされ、wp_localize_script( https://codex.wordpress.org/Function_Reference/wp_localize_script )または他のメソッドを使用して新しい投稿IDをJavaScriptに渡すことができます。

似たようなものが必要でしたが、クラスで使用されました。

class Foo {
    // this will hold the id of the new post
    private $postId = null;

    public function __construct()
    {
        // the actions are triggered in this order
        add_action('admin_enqueue_scripts', array($this, 'EnqueueScripts'));
        add_action('save_post', array($this, 'SavePost'));
        add_action('admin_print_scripts', array($this, 'LocalizeScripts'));
    }

    // enqueue your scripts and set the last parameter($in_footer) to true
    public function EnqueueScripts()
    {
        wp_enqueue_script('myJs', 'js/my.js', array('jquery'), false, true); 
    }

    // use wp_localize_script to pass to your script the post id
    public function LocalizeScripts()
    {
        wp_localize_script('myJs', 'myJsObject', array('postId'=>$this->GetPostId()));
    }

    // if $post_id is different from global post id you are in the write/create post page, else you are saving an existing post
    public function SavePost($post_id)
    {
        if($post_id != $this->GetPostId())
        {
            $this->SetPostId($post_id);
        }
    }

    private function GetPostId()
    {
        if (!$this->postId) {
            global $post;
            if($post)
            {
                $this->SetPostId($post->ID);
            }
        }

        return $this->postId;
    }

    private function SetPostId($postId)
    {
        $this->postId = $postId;
    }
}

これで、JavaScriptで次のものを使用できます。

myJsObject.postId
2
Dan Fluture

呼び出す正しいフック、またはwordpress adminライフサイクルの多くのフックの1つ)をまだ疑問に思っている人は、次のようになります。 admin_head

次のように:

function myplugin_setup() {
    global $post;
    $id = $post->ID;
    var_dump($id);
}

add_action('admin_head', 'myplugin_setup' );
1
Mel Macaluso

新しい投稿/ページの場合、投稿が公開/ DBに追加されていないため、IDはまだ存在しないと思います。投稿/ページを編集しようとしている場合は、$id = $_GET['post'];を使用できると思います。

0
bingjie2680