توضیحات
// 1) افزودن متاباکس عکس نویسنده به محصول
add_action(‘add_meta_boxes’, function () {
add_meta_box(
‘author_image_meta_box’,
‘عکس نویسنده’,
‘render_author_image_meta_box’,
‘product’,
‘side’,
‘default’
);
});
function render_author_image_meta_box($post) {
wp_nonce_field(‘save_author_image_meta_box’, ‘author_image_meta_box_nonce’);
$image_id = get_post_meta($post->ID, ‘_author_image_id’, true);
$image_url = $image_id ? wp_get_attachment_image_url($image_id, ‘thumbnail’) : ”;
?>
wp_enqueue_media();
}
});
// 3) ذخیره عکس نویسنده
add_action(‘save_post_product’, function ($post_id) {
if (!isset($_POST[‘author_image_meta_box_nonce’]) || !wp_verify_nonce($_POST[‘author_image_meta_box_nonce’], ‘save_author_image_meta_box’)) {
return;
}
if (defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE) {
return;
}
if (!current_user_can(‘edit_post’, $post_id)) {
return;
}
$image_id = isset($_POST[‘author_image_id’]) ? absint($_POST[‘author_image_id’]) : 0;
update_post_meta($post_id, ‘_author_image_id’, $image_id);
});
// 4) شورتکد برای نمایش عکس نویسنده
add_shortcode(‘author_image’, function ($atts) {
$atts = shortcode_atts([
‘size’ => ‘large’,
‘class’ => ‘author-image’,
], $atts);
$post_id = get_the_ID();
if (!$post_id) return ”;
$image_id = get_post_meta($post_id, ‘_author_image_id’, true);
if (!$image_id) return ”;
return wp_get_attachment_image($image_id, $atts[‘size’], false, [‘class’ => esc_attr($atts[‘class’])]);
});
// 5) خروجی برای Dynamic Tags به صورت URL
add_shortcode(‘author_image_url’, function () {
$post_id = get_the_ID();
if (!$post_id) return ”;
$image_id = get_post_meta($post_id, ‘_author_image_id’, true);
if (!$image_id) return ”;
return wp_get_attachment_image_url($image_id, ‘full’);
});
دیدگاهها
هیچ دیدگاهی برای این محصول نوشته نشده است.