prosource

워드프레스 add_meta_box() 이상함

probook 2023. 2. 8. 18:04
반응형

워드프레스 add_meta_box() 이상함

아래 코드는 거의 완벽하게 작동하고 있지만 몇 번 페이지를 새로 고친 후에도 페이지 제목 값이 계속 비어 있습니다.잠시 동안 고착되었다가 다시 빈 상태로 재설정됩니다.아래 코드에 컨플릭트가 있을 것 같은데 잘 모르겠어요.

유저가 투고용의 커스텀 페이지 타이틀과 커스텀 「투고/페이지 타이틀 입력 필드」를 개입시켜 페이지를 설정할 수 있도록 하고 있습니다.여기서 페이지 제목을 공백으로 리셋하는 명백한 문제를 알 수 있는 사람이 있습니까?

// ===================
// = POST OPTION BOX =
// ===================

add_action('admin_menu', 'my_post_options_box');

function my_post_options_box() {
    if ( function_exists('add_meta_box') ) { 
      //add_meta_box( $id, $title, $callback, $page, $context, $priority );
        add_meta_box('post_header', 'Custom Post Header Code (optional)', 'custom_post_images', 'post', 'normal', 'low');
        add_meta_box('post_title', 'Custom Post Title', 'custom_post_title', 'post', 'normal', 'high');
        add_meta_box('post_title_page', 'Custom Post Title', 'custom_post_title', 'page', 'normal', 'high');
        add_meta_box('postexcerpt', __('Excerpt'), 'post_excerpt_meta_box', 'page', 'normal', 'core');
        add_meta_box('categorydiv', __('Page Options'), 'post_categories_meta_box', 'page', 'side', 'core');
    }
}

//Adds the custom images box
function custom_post_images() {
    global $post;
    ?>
    <div class="inside">
        <textarea style="height:70px; width:100%;margin-left:-5px;" name="customHeader" id="customHeader"><?php echo get_post_meta($post->ID, 'customHeader', true); ?></textarea>
        <p>Enter your custom html code here for the post page header/image area. Whatever you enter here will override the default post header or image listing <b>for this post only</b>. You can enter image references like so &lt;img src='wp-content/uploads/product1.jpg' /&gt;. To show default images, just leave this field empty</p>
    </div>
<?php
}

//Adds the custom post title box
function custom_post_title() {
    global $post;
    ?>
    <div class="inside">
        <p><input style="height:25px;width:100%;margin-left:-10px;" type="text" name="customTitle" id="customTitle" value="<?php echo get_post_meta($post->ID, 'customTitle', true); ?>"></p>
        <p>Enter your custom post/page title here and it will be used for the html &lt;title&gt; for this post page and the Google link text used for this page.</p>
    </div>
<?php
}


add_action('save_post', 'custom_add_save');

function custom_add_save($postID){
    if (!defined('DOING_AUTOSAVE') && !DOING_AUTOSAVE) {
        return $postID;
    }
    else
    {
        // called after a post or page is saved and not on autosave
        if($parent_id = wp_is_post_revision($postID))
        {
        $postID = $parent_id;
        }

        if ($_POST['customHeader']) 
        {
            update_custom_meta($postID, $_POST['customHeader'], 'customHeader');
        }
        else
        {
            update_custom_meta($postID, '', 'customHeader');
        }
        if ($_POST['customTitle']) 
        {
            update_custom_meta($postID, $_POST['customTitle'], 'customTitle');
        }
        else
        {
            update_custom_meta($postID, '', 'customTitle');
        }
    }

  }
    function update_custom_meta($postID, $newvalue, $field_name) {
    // To create new meta
    if(!get_post_meta($postID, $field_name)){
    add_post_meta($postID, $field_name, $newvalue);
    }else{
    // or to update existing meta
    update_post_meta($postID, $field_name, $newvalue);
    }
}
?>

Wordpress의 자동 저장 시스템은 사용자의 문제일 수 있습니다. 사용자 지정 필드는 자동 저장을 위해 전달되지 않습니다.customHeader그리고.customTitle자동 저장 중에는 post 변수가 비어 있습니다).

저장 기능에서 다음과 같은 기능이 있는지 확인해야 합니다.DOING_AUTOSAVEconstant가 설정되어 있는 경우(이것은 포스트 액션을 체크하는 것보다 바람직하다고 생각됩니다).다음과 같은 경우:

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

상세한 것에 대하여는, 다음의 티켓을 참조해 주세요.

WordPress Codex에는 add_meta_box()에 대한 함수 참조가 포함되어 있습니다.

그리고 네, 그것은


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

올바른 방법으로 구현했다고 생각합니다.

참고로, http://wordpress.org/support/topic/custom-post-type-information-disappearing에 게재된 솔루션은 저에게 효과가 있었고, 저는 훨씬 더 우아하다고 생각합니다.

언급URL : https://stackoverflow.com/questions/2539951/wordpress-add-meta-box-weirdness

반응형