代码之家  ›  专栏  ›  技术社区  ›  Vishnu

删除插件后的页面停用代码不工作

  •  0
  • Vishnu  · 技术社区  · 6 年前

    我试图删除页面后插件停用。代码不工作的删除页面。

    register_activation_hook( __FILE__, 'insert_page' );
    
    function insert_page(){
    // Create post object
    $my_post = array(
      'post_title'    => 'Menu',
      'post_content'  => 'Short Code',
      'post_status'   => 'publish',
      'post_author'   => get_current_user_id(),
      'post_type'     => 'page',
    );
    
    // Insert the post into the database
    wp_insert_post( $my_post, '' );
     $newvalue = wp_insert_post( $post, false );
    update_option( 'Menu', $newvalue );
    }
    register_deactivation_hook( __FILE__, 'deactivate_plugin' );
    
    function deactivate_plugin() {
    
    $page = get_page_by_title('Menu');
    wp_delete_post($page); 
    
    }
    

    我甚至试过 wp_delete_post($page, true); . 它也不起作用。在我错的地方帮帮我。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Xhynk    6 年前

    如果你不介意的话,我想说几件事:

    首先,您应该处理代码缩进/格式化。 未来你谢谢你 !

    第二个问题是 wp_insert_post() 功能两次,但只工作一次。你也可以检查一下 WP1插入后() 调用前返回真值 update_option() .

    第三,小心“公共”函数名,因为你有可能重新声明错误!试用使用 namespaces , classes 或者至少在前缀你的函数名称以避免未来的头痛。

    第四,你不需要设置 $error 参数到 false WP1插入后() 因为这是它的默认值。

    最后,回答你的问题(最后!)它不起作用是因为 wp_delete_post() 需要一个职位 身份证件 你在路过一个岗位 目标 . 相反你会想用 $page->ID . 这是您的代码以及我在上面概述的更改:

    register_activation_hook( __FILE__, 'so_50960355_insert_page' );
    function so_50960355_insert_page(){
        // Define my page arguments
        $page = array(
            'post_title'   => 'Menu',
            'post_content' => 'Short Code',
            'post_status'  => 'publish',
            'post_author'  => get_current_user_id(),
            'post_type'    => 'page',
        );
    
        if( $page_id = wp_insert_post( $page ) ){
            // Only update this option if `wp_insert_post()` was successful
            update_option( 'my_menu_page_id', $page_id );
        }
    }
    
    register_deactivation_hook( __FILE__, 'so_50960355_delete_page' );
    function so_50960355_delete_page(){
        $page_id = intval( get_option( 'my_menu_page_id' ) );
    
        // Force delete this so the Title/slug "Menu" can be used again.
        wp_delete_post( $page_id, true );
    }