이번에는 wp_ajax를 호출하는 wordpress meta_box를 추가 할 것입니다.아약스가있는 Wordpress 메타 박스
시도했지만 작동하지 않습니다.
add_action('add_meta_boxes', 'add_custom_meta_box', 'post_type');
function add_custom_meta_box($post_type) {
if ($post_type == 'minisites') {
add_meta_box('sections_meta_box', 'Add Section', 'show_custom_meta_box');
// echo Utils::createStructureBox();
}
if ($post_type == 'sections') {
add_meta_box('sections_structure_box', 'Section Structure', 'show_section_structure_box');
}
}
function show_custom_meta_box() {
echo Utils::createSelect();
echo Utils::includeScripts();
}
function show_section_structure_box() {
echo "Done";
}
add_action('wp_ajax_addStructureBox', 'addStructureBox');
function addStructureBox() {
add_custom_meta_box('sections');
}
및 JQuery와는
$(document).ready(function() {
$('.addSection').on('click', function() {
var selectedSection = $('#sections option:selected').text();
$.post(ajaxurl, {action: 'addStructureBox', section: selectedSection}, function (data) {
alert(data);
});
});
});
이 좋아 보이지만 addStructureBox에 add_custom_meta_box를 호출하지 분명히 작동하지.
어떤 아이디어가 있습니까? HTML 코드
public static function createSelect() {
$sections = get_posts(array(
'post_type' => 'sections')
);
$html = '<select id="sections" name="item[]">';
foreach ($sections as $section) {
$html .= '<option value="' . $section->ID . '">' . $section->post_title . '</option>';
}
$html .= '</select><br><br>';
$html .= '<input type="button" class="button button-primary button-large addSection" id="add" value="Add"></button>';
return $html;
}
편집 : 스크린 샷
왜 ajax를 통해 metabox를 원합니까? – madalinivascu
첫 번째 메타 상자에서 섹션 단추 추가를 클릭하면 메타 상자를 추가해야합니다. –
메타 상자를 미리로드하지 않는 이유는 무엇입니까? – madalinivascu