2014-04-03 8 views
7

मैं अपनी पहली WP प्लगइन पर काम कर रहा हूं, और अटक गया हूं।वर्डप्रेस कॉलबैक फ़ंक्शन से पोस्ट मेटा प्राप्त करें

मैंने सामग्री संपादक के नीचे पोस्ट पेज पर एक कस्टम फ़ील्ड (फ़ील्ड 1) बनाया है। यह सही ढंग से बचाता है। :)

मैंने मीडिया जोड़ने के दौरान मीडिया लाइब्रेरी पॉपअप के अंदर एक कस्टम फ़ील्ड (फ़ील्ड 2) बनाया। यह सही ढंग से बचाता है। :)

मुझे क्या करना चाहते हैं रहा हूँ, क्षेत्र के लिए डिफ़ॉल्ट मान के रूप में क्षेत्र 1 से मान का उपयोग है 2.

मैं शक कर रहा हूँ कि समस्या attachment_fields_to_edit कॉलबैक फ़ंक्शन के भीतर निहित है ।

मुझे लगता है कि $ पद अब वास्तविक "फ़ाइल अनुलग्नक पोस्ट" करने के लिए बात कर रहा है बल्कि पद से भी है, इसलिए जब मैं अपने को बचाया मूल्यों को संदर्भित कर रहा हूँ:

$post_meta = get_post_meta($post->ID); 

यह वास्तव में सभी के खींच रहा है उस अनुलग्नक से जुड़े मेटा, और वर्तमान पोस्ट के साथ नहीं। क्या वास्तविक पोस्ट से मेटा खींचना संभव है?

इस कोड कोडेक्स से है:

function my_add_attachment_location_field($form_fields, $post) { 
    $field_value = get_post_meta($post->ID, 'location', true); 
    $form_fields['location'] = array(
     'value' => $field_value ? $field_value : '', 
     'label' => __('Location'), 
     'helps' => __('Set a location for this attachment') 
    ); 
    return $form_fields; 
} 
add_filter('attachment_fields_to_edit', 'my_add_attachment_location_field', 10, 2); 

function my_save_attachment_location($attachment_id) { 
    if (isset($_REQUEST['attachments'][$attachment_id]['location'])) { 
     $location = $_REQUEST['attachments'][$attachment_id]['location']; 
     update_post_meta($attachment_id, 'location', $location); 
    } 
} 
add_action('edit_attachment', 'my_save_attachment_location'); 

मैं वर्तमान पोस्ट है कि हम में लगाव डालने कर रहे हैं के लिए कैसे get_post_meta हैं? उपरोक्त कोडेक्स कोड में my_add_attachment_location_field कॉलबैक फ़ंक्शन में ऐसा होने की आवश्यकता होगी।

धन्यवाद!

+0

क्या आपके पास अपने डेटाबेस तक पहुंच है? – David

+0

वास्तव में आप इसे विशेष रुप से प्रदर्शित छवि के रूप में या अतिरिक्त छवियों के लिए कहां उपयोग करना चाहते हैं? – David

+0

अतिरिक्त छवियाँ – Aaron

उत्तर

1

एक तरह से मैं के बारे में सोच सकते हैं:

$ actual_post_id = $ पोस्ट> post_parent;

तो फिर तुम कर सकते हैं:

get_post_meta ($ actual_post_id)

+0

धन्यवाद, लेकिन ऐसा लगता है कि यह काम नहीं कर रहा है। मैं भी काफी कुछ खोज रहा हूं और कोई प्रगति नहीं कर सकता। – Aaron

+0

हमम ... मैं var_dump ($ पोस्ट) होगा और देखें कि post_parent गुण वास्तव में सेट है या नहीं। यदि यह अभी भी काम नहीं करता है, तो इस https://codex.wordpress.org/Function_Reference/get_post_ancestors आज़माएं। – Edenbauer

+0

धन्यवाद! मुझे लगता है कि $ पोस्ट वास्तव में छवि/अनुलग्नक के लिए विशिष्ट रूप से एक अलग "पोस्ट" है - और वास्तविक पोस्ट ही नहीं? मैं इस वजह से मीडिया लाइब्रेरी पॉपअप के भीतर वास्तविक पोस्ट आईडी को गूंजने में असमर्थ हूं। ऐसा लगता है कि आपके द्वारा संदर्भित फ़ंक्शन पोस्ट आईडी चाहते हैं, लेकिन मैं इसे एक शॉट दूंगा। – Aaron

1

आप कोशिश कर सकते हैं निम्नलिखित:

/** 
* Display custom 'location' attachment field 
*/ 

function so_22850878_attachment_fields_to_edit($form_fields, $post) 
{ 
    $field_value = get_post_meta($post->post_parent, 'location', true); 

    $form_fields['location'] = array(
     'value' => $field_value ? $field_value : '', 
     'label' => __('Location'), 
     'helps' => __('Set a location for this attachment') 
    ); 

    return $form_fields; 
} 

add_filter('attachment_fields_to_edit', 
      'so_22850878_attachment_fields_to_edit', 10, 2); 

और

/** 
* Edit attachment fields 
*/ 

function so_22850878_edit_attachment($attachment_id) 
{ 
    $p = get_post($attachment_id); 

    if (isset($_REQUEST['attachments'][$attachment_id]['location'])) 
    { 
     $location = $_REQUEST['attachments'][$attachment_id]['location']; 

     // Save the value of the 'location' attachment field 
     // to the 'location' post meta of the post parent if it exists: 

     if(! is_null($p) 
      && 0 < $p->post_parent 
     ) 
      update_post_meta($p->post_parent, 
           'location', 
           sanitize_text_field($location) 
      ); 
    } 
} 

add_action('edit_attachment', 'so_22850878_edit_attachment'); 
मीडिया पॉपअप से, पेरेंट पोस्ट के location पोस्ट मेटा मान को अपडेट करने के लिए

आप भी इस की जाँच करने के लिए, यदि आप मीडिया लाइब्रेरी पृष्ठ से सीधे लगाव संपादित चाह सकते हैं:

/** 
* Save attachment fields 
*/ 

function so_22850878_attachment_fields_to_save($post, $attachment) 
{ 
    $p = get_post($post['ID']); 

    // Save the value of the 'location' attachment field 
    // to the 'location' post meta of the post parent if it exists: 

    if(isset($attachment['location']) 
     && ! is_null($p) 
     && 0 < $p->post_parent 
    ) 
     update_post_meta($p->post_parent, 
          'location', 
          sanitize_text_field($attachment['location']) 
     ); 

    return $post; 
} 

add_action('attachment_fields_to_save', 
      'so_22850878_attachment_fields_to_save', 10, 2); 

मुझे यकीन है कि कार्यप्रवाह आप किस तरह के मन में है नहीं कर रहा हूँ, लेकिन मुझे लगता है अपने विचार के साथ एक समस्या है, मैं यह समझ के रूप में:

आप मीडिया पॉपअप में location क्षेत्र अद्यतन करते हैं, यह आप माता-पिता इस पद के लिए location पोस्ट मेटा मूल्य अपडेट करना चाहते हैं की तरह लग रहा है। लेकिन जब आप पोस्ट संपादक में छवि डालते हैं तो पोस्ट एडिट स्क्रीन अपडेट नहीं होती है, इसलिए location मान पोस्ट को अपडेट करते समय पुराने मान के साथ ओवरराइट किया जाएगा।

तो मुझे आश्चर्य है अगर आप उदाहरण _location के लिए बजाय एक छिपे हुए पद मेटा मूल्य इस्तेमाल कर सकते हैं?

उम्मीद है कि इससे मदद मिलती है।

0

ठीक है एक अलग तरीके से प्रयास करें ...... आप नेविगेटर में पोस्ट आईडी प्राप्त करने में आसानी से सक्षम नहीं होंगे। इम यकीन नहीं क्या स्थान है, लेकिन यदि आप पोस्ट मेटा के रूप में छवियों को बचाना चाहते हैं, मैं इस का उपयोग ............

कदम:

1. बनाएँ एक नया js फ़ाइल + http://jsfiddle.net/dheffernan/BB37U/2/ पर जाएं और जेएस फ़ाइल में जेएस की प्रतिलिपि बनाएँ। इसे miu_script.js पर कॉल करें या यदि आप इसे बदलना चाहते हैं तो आपको नीचे दिए गए कोड में कुछ मोड बनाने की आवश्यकता है। अपने प्लगइन फ़ोल्डर (नीचे परिवर्तन रास्ते में इसे सहेजें यदि आप इसे सबफ़ोल्डर में ले जाना चाहते हैं

  1. कोई कोड डाल अपने कार्यों में नीचे -। यह एक धारावाहिक यूआरएल के रूप में और एक क्षेत्र कहा जाता में छवि स्थान की बचत होगी यदि आप कोड को संशोधित करना चाहते हैं तो "_images" फिर से बदल जाएंगे।

  2. नीचे त्रुटियां हो सकती हैं, मेरे पास ओओपी प्रारूप में था इसलिए मुझे बताएं कि क्या समस्याएं हैं। अगर वहां हैं, तो PHP त्रुटियों को नोट करें, यदि नहीं php त्रुटियों लेकिन यह dosent काम, क्रोम या फ़ायरफ़ॉक्स में सांत्वना की जाँच करें। बीमार डिबग करने में सक्षम हो।

अपने कार्यों php

function add_image_meta_box() { 
    add_meta_box(
       'multi_image_upload_meta_box' 
       , __('Upload Multiple Images', 'miu_textdomain') 
       , 'render_meta_box_content' 
       , $post_type 
       , 'advanced' 
       , 'high' 
     ); 
} 

add_action('add_meta_boxes', 'add_image_meta_box'); 




function render_meta_box_content($post) { 

    wp_nonce_field('miu_inner_custom_box', 'miu_inner_custom_box_nonce'); 

    $value = get_post_meta($post->ID, '_images', true); // <-- change field if wanted, there is 1 more below that will need the same name 

    $metabox_content = '<div id="miu_images"></div><input type="button" onClick="addRow()" value="Add Image" class="button" />'; 
    echo $metabox_content; 

    $images = unserialize($value); //<--- when using the images use this!! 

    $script = "<script> 
     itemsCount= 0;"; 
    if (!empty($images)) 
    { 
     foreach ($images as $image) 
     { 
      $script.="addRow('{$image}');"; 
     } 
    } 
    $script .="</script>"; 
    echo $script; 
} 





function save_image($post_id){ 

    if (!isset($_POST['miu_inner_custom_box_nonce'])) 
     return $post_id; 

    $nonce = $_POST['miu_inner_custom_box_nonce']; 

    if (!wp_verify_nonce($nonce, 'miu_inner_custom_box')) 
     return $post_id; 

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
     return $post_id; 


    if ('page' == $_POST['post_type']) { 

     if (!current_user_can('edit_page', $post_id)) 
      return $post_id; 
    } else { 

     if (!current_user_can('edit_post', $post_id)) 
      return $post_id; 
    } 

    $posted_images = $_POST['miu_images']; 
    $miu_images = array(); 
    foreach ($posted_images as $image_url) { 
     if(!empty ($image_url)) 
      $miu_images[] = esc_url_raw($image_url); 
    } 


    update_post_meta($post_id, '_images', serialize($miu_images));<--if you changed this above.......make sure they match 
    } 

    add_action('save_post', 'save_image'); 


    function enqueue_scripts($hook){ 
    if ('post.php' != $hook && 'post-edit.php' != $hook && 'post-new.php' != $hook) 
     return; 
    wp_enqueue_script('miu_script', plugin_dir_url(__FILE__) . 'miu_script.js', array('jquery')); //<--this is the path!!! change if wanted (prob a good idea to add to js folder) 
} 
    add_action('admin_enqueue_scripts', 'enqueue_scripts'); 
0

सिर्फ एक सरल समाधान में

function my_add_attachment_location_field($form_fields, $post) { 
    $field_value = get_post_meta($post->ID, 'location', true); 

    $default = get_post_meta($_REQUEST['post_id'] , 'location', true) ? get_post_meta($_REQUEST['post_id'] , 'location', true): "Location Not Yet Set"; 

    $form_fields['location'] = array(
     'value' => $field_value ? $field_value : $default, 
     'label' => __('Location'), 
     'helps' => __('Set a location for this attachment') 
    ); 
    return $form_fields; 
} 
add_filter('attachment_fields_to_edit', 'my_add_attachment_location_field', 10, 2); 

मेरी प्लगइन

$ _REQUEST [ 'post_id'] के साथ ठीक काम कर रहा वास्तविक WP पोस्ट आईडी है और get_post_meta के साथ आपका मेटाबोक्स स्थान प्राप्त कर सकते हैं .. :)

0

मुझे नहीं पता कि मैं बस अनुमान लगा रहा हूं या नहीं। आप इस फ़ंक्शन का उपयोग कर सकते हैं

wp_reset_query(); 
    global $post; 
    $post->ID // retrive the global $post id; 
संबंधित मुद्दे