সম্পাদকের উপরে কিন্তু শিরোনাম বিভাগের নীচে সম্পাদনা পোস্ট পৃষ্ঠায় আমি কীভাবে একটি কাস্টম মেটা বক্স রাখতে পারি?


30

আমার কাছে একটি কাস্টম পোস্ট টাইপের জন্য একটি কাস্টম মেটা বক্স রয়েছে যা আমার ক্লায়েন্টটি অ্যাডমিন প্যানেলে শিরোনাম / পারমালিঙ্ক বিভাগ এবং পোস্ট সম্পাদকের মধ্যে রাখতে চায়। এটি কি সম্ভব এবং যদি এমন কোনও হুক / ফিল্টার / ইত্যাদি থাকে যা আমার ব্যবহারের প্রয়োজন?


উত্তর:


51
  • উন্নত প্রসঙ্গ এবং উচ্চ অগ্রাধিকার ব্যবহার করে কেবল একটি মেটা বক্স যুক্ত করুন
  • তারপরে, edit_form_after_titleহুকের উপর ল্যাচ করুন
  • আপনার মেটা বাক্সগুলি সেখানে মুদ্রণ করুন, তারপরে এটি সরিয়ে ফেলুন যাতে এটি দু'বার প্রদর্শিত না হয়।

    // Move all "advanced" metaboxes above the default editor
    add_action('edit_form_after_title', function() {
        global $post, $wp_meta_boxes;
        do_meta_boxes(get_current_screen(), 'advanced', $post);
        unset($wp_meta_boxes[get_post_type($post)]['advanced']);
    });
    

একটি সাইট যা আমি কাজ করছি ফাংশনের register_meta_box_cbপ্যারামিটার ব্যবহার করে কিছু মেটাবক্স নিবন্ধিত করে register_post_type। আমি আপনার কোড চেষ্টা করেছি কিন্তু মেটাবক্সগুলি সম্পাদকের উপরে চলে যায় না। এটি কি আমার ক্ষেত্রে ব্যবহার করা যেতে পারে? ধন্যবাদ
28:57

আমি একটি কাস্টম ব্যবহার করার $contextপরিবর্তে এর advancedমতো কিছু ব্যবহার করার পরামর্শ দেব my_before_editor, যাতে আপনি সমস্ত মেটা বাক্স advancedপ্রসঙ্গে না সরিয়ে , আপনি বিশেষভাবে আপনার নির্দিষ্ট মেটা বাক্সগুলিকে লক্ষ্যবস্তু করেন .. দেখুন ডেভেলপার.ওয়ার্ডপ্রেস.আর
রেফারেন্স

14

এখানে আপনি সম্পাদকের উপরে নির্দিষ্ট মেটা বাক্সগুলি কীভাবে সরাতে পারবেন তা এখানে রয়েছে তবে কোড পোস্ট করার আগে আমি কেবল অ্যান্ড্রু এবং মুলসকে ধন্যবাদ জানাতে চাই। তোমরা অসাধারণ!

function foo_deck( $post_type ) {
    if ( in_array( $post_type, array( 'post', 'page' ) ) ) {
        add_meta_box(
            'contact_details_meta',
            'Contact Details',
            'contact_details_meta',
            $post_type,
            'test', // change to something other then normal, advanced or side
            'high'
        );
    }
}

add_action('add_meta_boxes', 'foo_deck');

function foo_move_deck() {
        # Get the globals:
        global $post, $wp_meta_boxes;

        # Output the "advanced" meta boxes:
        do_meta_boxes( get_current_screen(), 'test', $post );

        # Remove the initial "advanced" meta boxes:
        unset($wp_meta_boxes['post']['test']);
    }

add_action('edit_form_after_title', 'foo_move_deck');

1
change to something other then normal, advanced or side- আমার ক্ষেত্রে চাবি। তথ্যের জন্য ধন্যবাদ।
মায়নুল ইসলাম

এটি আমার কাছে সবচেয়ে সহায়ক উত্তর ছিল। ধন্যবাদ!
মার্ভিনপু

12

অ্যান্ড্রুয়ের উত্তরের ভিত্তিতে একটি পূর্ণ কোড উদাহরণ সরবরাহ করতে ... আমার পোস্টগুলিতে "ডেক" (ওরফে সাবহেড) অন্তর্ভুক্ত করার জন্য আমার একটি উপায় প্রয়োজন; আমি চেয়েছিলাম যে ডেক ক্ষেত্রটি প্রধান শিরোনাম বারের পরে প্রদর্শিত হবে।

/**
 * Add a "deck" (aka subhead) meta box to post page(s) and position it
 * under the title.
 *
 * @todo Move to class.
 * @see http://codex.wordpress.org/Function_Reference/add_meta_box
 * @see http://wordpress.org/extend/ideas/topic/add-meta-box-to-multiple-post-types
 * @see https://github.com/Horttcore/WordPress-Subtitle
 * @see http://codex.wordpress.org/Function_Reference/wp_nonce_field
 */

# Adds a box to the main column on the Post and Page edit screens:
function foo_deck($post_type) {

    # Allowed post types to show meta box:
    $post_types = array('post', 'page');

    if (in_array($post_type, $post_types)) {

        # Add a meta box to the administrative interface:
        add_meta_box(
            'foo-deck-meta-box', // HTML 'id' attribute of the edit screen section.
            'Deck',              // Title of the edit screen section, visible to user.
            'foo_deck_meta_box', // Function that prints out the HTML for the edit screen section.
            $post_type,          // The type of Write screen on which to show the edit screen section.
            'advanced',          // The part of the page where the edit screen section should be shown.
            'high'               // The priority within the context where the boxes should show.
        );

    }

}

# Callback that prints the box content:
function foo_deck_meta_box($post) {

    # Use `get_post_meta()` to retrieve an existing value from the database and use the value for the form:
    $deck = get_post_meta($post->ID, '_deck', true);

    # Form field to display:
    ?>

        <label class="screen-reader-text" for="foo_deck">Deck</label>
        <input id="foo_deck" type="text" autocomplete="off" value="<?=esc_attr($deck)?>" name="foo_deck" placeholder="Deck">

    <?php

    # Display the nonce hidden form field:
    wp_nonce_field(
        plugin_basename(__FILE__), // Action name.
        'foo_deck_meta_box'        // Nonce name.
    );

}

/**
 * @see https://wordpress.stackexchange.com/a/16267/32387
 */

# Save our custom data when the post is saved:
function foo_deck_save_postdata($post_id) {

    # Is the current user is authorised to do this action?
    if ((($_POST['post_type'] === 'page') && current_user_can('edit_page', $post_id) || current_user_can('edit_post', $post_id))) { // If it's a page, OR, if it's a post, can the user edit it? 

        # Stop WP from clearing custom fields on autosave:
        if ((( ! defined('DOING_AUTOSAVE')) || ( ! DOING_AUTOSAVE)) && (( ! defined('DOING_AJAX')) || ( ! DOING_AJAX))) {

            # Nonce verification:
            if (wp_verify_nonce($_POST['foo_deck_meta_box'], plugin_basename(__FILE__))) {

                # Get the posted deck:
                $deck = sanitize_text_field($_POST['foo_deck']);

                # Add, update or delete?
                if ($deck !== '') {

                    # Deck exists, so add OR update it:
                    add_post_meta($post_id, '_deck', $deck, true) OR update_post_meta($post_id, '_deck', $deck);

                } else {

                    # Deck empty or removed:
                    delete_post_meta($post_id, '_deck');

                }

            }

        }

    }

}

# Get the deck:
function foo_get_deck($post_id = FALSE) {

    $post_id = ($post_id) ? $post_id : get_the_ID();

    return apply_filters('foo_the_deck', get_post_meta($post_id, '_deck', TRUE));

}

# Display deck (this will feel better when OOP):
function foo_the_deck() {

    echo foo_get_deck(get_the_ID());

}

# Conditional checker:
function foo_has_subtitle($post_id = FALSE) {

    if (foo_get_deck($post_id)) return TRUE;

}

# Define the custom box:
add_action('add_meta_boxes', 'foo_deck');
# Do something with the data entered:
add_action('save_post', 'foo_deck_save_postdata');

/**
 * @see https://wordpress.stackexchange.com/questions/36600
 * @see https://wordpress.stackexchange.com/questions/94530/
 */

# Now move advanced meta boxes after the title:
function foo_move_deck() {

    # Get the globals:
    global $post, $wp_meta_boxes;

    # Output the "advanced" meta boxes:
    do_meta_boxes(get_current_screen(), 'advanced', $post);

    # Remove the initial "advanced" meta boxes:
    unset($wp_meta_boxes['post']['advanced']);

}

add_action('edit_form_after_title', 'foo_move_deck');

স্পষ্টতই, উপরের কোডটি আরও কিছু কাজ ব্যবহার করতে পারে, তবে এটি একই জিনিসগুলি করার চেষ্টা করা অন্যদের সহায়তা করা উচিত (অ্যান্ড্রুয়ের উত্তরটি আলোকে আলোকিত করেছিল, তবে আমি ভেবেছিলাম এটি সম্ভবত একটি কার্যকারী উদাহরণ দেওয়ার ক্ষেত্রে সহায়ক হতে পারে)।

এই উত্তরটিও সাহায্য করেছিল

যে উন্নতিগুলি করা যেতে পারে:

  1. ওওপি / ক্লাস (এসএস) করুন।
  2. শিরোনাম ক্ষেত্রের মতো দেখতে / বোধ / আচরণ করতে শৈলী / জেএস যুক্ত করুন।

আমি ভবিষ্যতে কোনও সময়ে উপরোক্ত উন্নতিগুলি করার পরিকল্পনা করছি, তবে কমপক্ষে উপরের কোডটি অন্যদের এটি বের করার চেষ্টা করা উচিত।

আরও অনুপ্রেরণার জন্য এখানে উত্স কোড দেখুন (তারা "উপ-শিরোনাম" সরানোর জন্য jQuery ব্যবহার করতে পছন্দ করেছেন)।


এটি যদি কাউকে একই পথে নামতে সহায়তা করে: আমি এখানে একটি প্রশ্ন জিজ্ঞাসা করেছি যার কিছু সম্পর্কিত / অনুরূপ কোড রয়েছে (আমি সাবথহেড ধরে রাখতে এবং ফিল্টার করার জন্য "শিরোনাম" ক্ষেত্রটি ব্যবহার করতে পছন্দ করেছি)।
mululse

6

উন্নত বিভাগের সমস্ত কিছুকে শীর্ষে স্থানান্তরিত করার পরিবর্তে কেন একটি নতুন বিভাগ তৈরি করে এটিকে শীর্ষে স্থানান্তরিত করবেন না:

// Create 'top' section and move that to the top
add_action('edit_form_after_title', function() {
  global $post, $wp_meta_boxes;
  do_meta_boxes(get_current_screen(), 'top', $post);
  unset($wp_meta_boxes[get_post_type($post)]['top']);
});

এখন আপনাকে যা করতে হবে তা হ'ল topবিভাগের highজন্য এবং অগ্রাধিকারের জন্য মেটা বক্সটি নিবন্ধিত করুন ।

এটি আমার জন্য ওয়ার্ডপ্রেস ৪.৪.২ এ কাজ করছে। আমি অন্যান্য ডাব্লুপি সংস্করণে এটি পরীক্ষা করিনি।


1

অন্য কোনও উপায় আছে, যেভাবে আমরা সম্পাদককে যে কোনও অবস্থাতে রাখতে পারি:

  1. আপনি পোস্ট_ টাইপ নিবন্ধভুক্ত করার সময় সমর্থন প্যারাম থেকে সম্পাদক সরান

  2. একটি জাল মেটাবক্স যুক্ত করুন

    add_meta_box( 'does-not-matter', 
    __( 'Description'), 
    function($post){ 
      wp_editor($post->post_content,'post_content',array('name'=>'post_content'));
    },
    'post_type_type', 
    'advanced', 
    'high' );

এফওয়াইআই, এটি এখনও কাজ করে তবে আপনি যখন বাক্সটি সরান এটি ক্ষেত্রের সামগ্রীর সাথে কিছু অদ্ভুত আচরণের কারণ হয়ে দাঁড়ায়। ব্যবহারকারীদের সাবধান।
একস্টাইন
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.