WordPress как на ладони
Очень Удобный и Быстрый Хостинг для сайтов на WordPress. Пользуюсь сам и вам рекомендую!

Быстрое редактирование страниц

Есть два типа страниц: Портфолио(portfolio) и Возможности (opportunities).
В Портфолио есть поле для быстрого редактирования поля Рейтинга.

Как сделать такое же поле для Возможностей?

'post_type' => array('portfolio', 'opportunities')
//Rating edit for portfolio

add_action( 'init', 'Rating_Meta_Bulk_Edit::get_instance' );
class Rating_Meta_Bulk_Edit {

	/**
	 * @var Rating_Meta_Bulk_Edit Instance of the class.
	 */
	private static $instance = false;

	/**
	 * @var array Configuration
	 */
	private $meta_settings = array(
		'title' => 'Рейтинг:', 
		'slug' => 'rating',
		'input_type' => 'input',
		'post_type' => 'portfolio',
		'empty_message' => '0' 
	);

	/**
	 * Don't use this. Use ::get_instance() instead.
	 */
	public function __construct() {
		if ( !self::$instance ) {
			$message = '' . __CLASS__ . 'is a singleton.
Please get an instantiate it with ' . __CLASS__ . '::get_instance();'; wp_die( $message ); } } public static function get_instance() { if ( !is_a( self::$instance, __CLASS__ ) ) { self::$instance = true; self::$instance = new self(); self::$instance->init(); } return self::$instance; } /** * Initial setup. Called by get_instance. */ protected function init() { add_action( 'admin_print_scripts-edit.php', array( $this, 'enqueue_edit_scripts' ) ); add_filter( 'manage_'.$this->meta_settings['post_type'].'_posts_columns', array( $this, 'edit_meta_columns' ), 1 ); add_action( 'manage_'.$this->meta_settings['post_type'].'_posts_custom_column', array( $this, 'manage_meta_columns' ), 10, 2 ); add_action( 'bulk_edit_custom_box', array( $this, 'add_to_bulk_quick_edit_custom_box' ), 10, 2 ); add_action( 'quick_edit_custom_box', array( $this, 'add_to_bulk_quick_edit_custom_box' ), 10, 2 ); add_action( 'save_post', array( $this, 'save_post' ), 10, 2 ); add_action( 'wp_ajax_storm_save_bulk_edit', array( $this, 'storm_save_bulk_edit' ) ); } /** * Enqueue js script for loading / saving Quick Edit / Bulk Edit */ function enqueue_edit_scripts() { wp_enqueue_script( 'storm-meta-bulk', get_stylesheet_directory_uri() . '/js/populate.js', array( 'jquery', 'inline-edit-post' ), '', true ); } /** * Add column for Meta on CPT */ function edit_meta_columns( $columns ) { $columns[$this->meta_settings['slug']] = $this->meta_settings['title']; return $columns; } /** * Populate column */ function manage_meta_columns( $column, $post_id ) { global $post; switch( $column ) { /* If displaying our custom meta column. */ case $this->meta_settings['slug'] : /* Get the post meta. */ $custom_meta = get_post_meta( $post_id, $this->meta_settings['slug'], true ); /* If no custom meta is found, output a default message. */ if ( empty( $custom_meta ) ) echo $this->meta_settings['empty_message']; else echo '
' . get_post_meta( $post_id, $this->meta_settings['slug'], true ) . '
'; break; default : break; } } /** * Input field output for bulk edit / quick edit */ function add_to_bulk_quick_edit_custom_box( $column_name, $post_type ) { switch ( $post_type ) { case $this->meta_settings['post_type']: switch( $column_name ) { case $this->meta_settings['slug']: ?>
post_type ) && $post->post_type == 'revision' ){ return $post_id; } switch( $post->post_type ) { case $this->meta_settings['post_type']: if ( array_key_exists( 'storm_meta_input', $_POST ) ) update_post_meta( $post_id, $this->meta_settings['slug'], $_POST[ 'storm_meta_input' ] ); break; } } /** * Ajax save quick edit / bulk */ function storm_save_bulk_edit() { // get our variables $post_ids = ( isset( $_POST[ 'post_ids' ] ) && !empty( $_POST[ 'post_ids' ] ) ) ? $_POST[ 'post_ids' ] : array(); $meta_value = ( isset( $_POST[ 'storm_meta_input' ] ) && !empty( $_POST[ 'storm_meta_input' ] ) ) ? $_POST[ 'storm_meta_input' ] : NULL; // if everything is in order if ( !empty( $post_ids ) && is_array( $post_ids ) && !empty( $meta_value ) ) { foreach( $post_ids as $post_id ) { update_post_meta( $post_id, $this->meta_settings['slug'], $meta_value ); } } } }
0
cyber-blend
4.9 лет назад
  • 1
    Glum697

    Если это кастомный класс, то можно так

    обновить свойство

        private $meta_settings = [
    		'title'         => 'Рейтинг:', 
    		'slug'          => 'rating',
    		'input_type'    => 'input',
    		'post_type'     => ['portfolio', 'opportunities'],
    		'empty_message' => '0' 
    	];

    потом в методе init() заменить

       add_filter( 'manage_'.$this->meta_settings['post_type'].'_posts_columns', array( $this, 'edit_meta_columns' ), 1 );
       add_action( 'manage_'.$this->meta_settings['post_type'].'_posts_custom_column', array( $this, 'manage_meta_columns' ), 10, 2 );  

    на

        add_filter( 'manage_'.$this->meta_settings['post_type'][0] . '_posts_columns', array( $this, 'edit_meta_columns' ), 1 );
    	add_action( 'manage_'.$this->meta_settings['post_type'][0] . '_posts_custom_column', array( $this, 'manage_meta_columns' ), 10, 2 );
    
    	add_filter( 'manage_'.$this->meta_settings['post_type'][1] . '_posts_columns', array( $this, 'edit_meta_columns' ), 1 );
    	add_action( 'manage_'.$this->meta_settings['post_type'][1] . '_posts_custom_column', array( $this, 'manage_meta_columns' ), 10, 2 );

    и в методе add_to_bulk_quick_edit_custom_box() добавить несколько кейсов и изменить проверку в них с
    case $this->meta_settings['post_type'] :
    на case $this->meta_settings['post_type'][0] : и case $this->meta_settings['post_type'][1] : соответственно

    Комментировать
На вопросы могут отвечать только зарегистрированные пользователи. Вход . Регистрация