在Add_Filter()中获取帖子ID

时间:2016-11-23 作者:knmsuser

如何在管理中获取当前帖子id?我想使用基于帖子ID的自定义上传目录,但我似乎无法检索它?

add_filter( \'wp_handle_upload_prefilter\', \'do_pre_upload\' );

function do_pre_upload( $file ) {
add_filter( \'upload_dir\', \'do_custom_upload_dir\' );
        return $file;
}

function do_custom_upload_dir( $param ) {

    global $post; 
    $id = 344;  // HOW DO IT GET THE CURRENT POST ID
    //$id = $post->ID;  // DOESNT WORK DOESNT GET A VALUE?????


    $parent = get_post( $id )->post_parent;
    if( "client_gallery" == get_post_type( $id ) || "client_gallery" == get_post_type( $parent ) ) {
        $mydir         = \'/client_galleryy/\'.$post->ID.\'\';
        $param[\'path\'] = $param[\'basedir\'] . $mydir;
        $param[\'url\']  = $param[\'baseurl\'] . $mydir;
    }



   return $param;


}

UPDATE

这是我的插件的完整代码,它创建了我的自定义帖子类型,使用meta-box插件创建了一个meta-box,现在是为我的自定义帖子类型/帖子id设置上传目录的代码。我必须将上传目录的代码放在错误的位置,因为它不能确定im在我的自定义帖子类型中,它应该为帖子创建新的上传目录?

**
 * Plugin Name: My Plugin Name
 * Plugin URI: domain.com
 * Description: my description
 * Version: 1.0.0
 * Author: My Name
 * Author URI: domain.com
 * License: GPL2
 */





// START CREATE CUSTOM POST TYPE
add_action( \'init\', \'create_post_type\' );
function create_post_type() {
    register_post_type( \'client_gallery\',
        array(
            \'labels\' => array(
                \'name\' => __( \'Galleries\' ),
                \'singular_name\' => __( \'Gallery\' )
            ),
            \'public\' => true,
            \'has_archive\' => true,
            \'supports\' => array( \'title\' ),
        )
    );




}
// END CREATE CUSTOM POST TYPE


// START CREATE META BOX
add_filter( \'rwmb_meta_boxes\', \'xyz_gallery_meta_boxes\' );
function xyz_gallery_meta_boxes( $meta_boxes ) {




    $meta_boxes[] = array(
        \'title\'      => __( \'Gallery Images\', \'textdomain\' ),
        \'post_types\' => \'client_gallery\',
        \'fields\'     => array(
    array(
        \'name\'             => esc_html__( \'Image\', \'textdomain\' ),
        \'id\'               => "clientImage",
        \'type\'             => \'image_advanced\',
    ),
        ),
    );


    return $meta_boxes;
}
// END CREATE META BOX






/**
 * Modify Upload Directory Based on Post Type
 *
 * @param Array $dir
 *
 * @return Array $dir
 */
function wpse_247197( $dir ) {
    $request_arr    = array( \'upload-attachment\', \'editpost\', \'editedtag\' );
    $request        = array_change_key_case( $_REQUEST, CASE_LOWER );   // WordPress uses post_id and post_ID
    $type           = null;

    // Are we where we want to be?
    if( ! ( isset( $_REQUEST[\'action\'] ) && in_array( $_REQUEST[\'action\'], $request_arr ) ) ) {
        return $dir;
    }

    if( isset( $request[\'post_id\'] ) ) {            // Get Post ID
        $id = $request[\'post_id\'];

        if( isset( $request[\'post_type\'] ) ) {      // Get the post type if it was passed
            $type = $request[\'post_type\'];
        } else {                                    // If not passed, get post type by ID
            $type = get_post_type( $id );
        }
    } else {                                        // If we can\'t get a post ID return default directory
        return $dir;
    }

    if( isset( $type, $id ) && in_array( $type, $post_type_arr ) ) {

        // Here we can test our type and change the directory name etc. if we really wanted to
        if( \'client_gallery\' != $type ) {
            return $dir;
        }

        $uploads     = apply_filters( "{$type}_upload_directory", "{$type}/{$id}" );        // Set our uploads URL for this type
        $dir[\'url\']  = path_join( $dir[\'baseurl\'], $uploads );                              // Apply the URL to the directory array
        $dir[\'path\'] = path_join( $dir[\'basedir\'], $uploads );                              // Apply the Path to the directory array
    }

    return $dir;
}
add_filter( \'upload_dir\', \'wpse_247197\' );
// END Modify Upload Directory Based on Post Type

3 个回复
最合适的回答,由SO网友:Howdy_McGee 整理而成

根据您的评论,听起来您只是想为您的帖子类型创建一个名为client_gallery 事实上这很直接。以下仅使用upload_dir 挂钩以实现此目的:

/**
 * Modify Upload Directory Based on Post Type
 *
 * @param Array $dir
 *
 * @return Array $dir
 */
function wpse_247197( $dir ) {
    $request_arr    = array( \'upload-attachment\', \'editpost\', \'editedtag\' );
    $request        = array_change_key_case( $_REQUEST, CASE_LOWER );   // WordPress uses post_id and post_ID
    $type           = null;

    // Are we where we want to be?
    if( ! ( isset( $_REQUEST[\'action\'] ) && in_array( $_REQUEST[\'action\'], $request_arr ) ) ) {
        return $dir;
    }

    if( isset( $request[\'post_id\'] ) ) {            // Get Post ID
        $id = $request[\'post_id\'];

        if( isset( $request[\'post_type\'] ) ) {      // Get the post type if it was passed
            $type = $request[\'post_type\'];
        } else {                                    // If not passed, get post type by ID
            $type = get_post_type( $id );
        }
    } else {                                        // If we can\'t get a post ID return default directory
        return $dir;
    }

    if( isset( $type, $id ) && ! empty( $type ) && ! empty( $id ) ) {

        // Here we can test our type and change the directory name etc. if we really wanted to
        if( \'product\' != $type ) {
            return $dir;
        }

        $uploads     = apply_filters( "{$type}_upload_directory", "{$type}/{$id}" );        // Set our uploads URL for this type
        $dir[\'url\']  = path_join( $dir[\'baseurl\'], $uploads );                              // Apply the URL to the directory array
        $dir[\'path\'] = path_join( $dir[\'basedir\'], $uploads );                              // Apply the Path to the directory array

        error_log( print_r( $dir, 1 ) );
    }

    return $dir;
}
add_filter( \'upload_dir\', \'wpse_247197\' );
我已经对上述代码进行了大量的注释,因此如果您对其有任何疑问,可以在注释中提出。其想法是,每当用户上传到帖子类型本身时,就会直接上传到文件夹。分配预上载的附件介质时情况并非如此,一旦文件已上载且URL已设置,则移动这些文件将非常麻烦。

SO网友:Benoti

$post 是一个对象,您可以像这样获取帖子id

  $id = $post->ID;

SO网友:GKS

您可以通过以下方式获取帖子ID$_REQUEST .

add_action(\'init\',function(){
   global $post;

   if($post){
       session_start();
       $_SESSION[\'p_id\'] = $post->ID;
   }

});

add_filter( \'wp_handle_upload_prefilter\', \'do_pre_upload\' );

function do_pre_upload( $file ) {

        add_filter( \'upload_dir\', \'do_custom_upload_dir\' );
        return $file;
}

function do_custom_upload_dir( $param ) {

    // Get the current post_id
    $id = ( isset( $_SESSION[\'p_id\'] ) ? $_SESSION[\'p_id\'] : \'\' );

    unset($_SESSION[\'p_id\']);

    $parent = get_post( $id )->post_parent;
    if( "client_gallery" == get_post_type( $id ) || "client_gallery" == get_post_type( $parent ) ) {
        $mydir         = \'/client_galleryy/\'.$post->ID.\'\';
        $param[\'path\'] = $param[\'basedir\'] . $mydir;
        $param[\'url\']  = $param[\'baseurl\'] . $mydir;
    }



   return $param;


}
试试看,应该可以。。。

相关推荐

Apply_Filters()对所需的参数进行切片

我正在尝试向WooCommerce订单中的每个退款行添加一个按钮(其功能超出了这个问题的范围,足以说明它需要退款id作为参数)。我发现这些行是在woocommerce\\includes\\admin\\meta Box\\views\\html订单退款中创建的。无法重写的php。然而,有一项行动:do_action( \'woocommerce_admin_order_item_values\', null, $refund, $refund->get_id() ); 这似乎非常适合我的