رفتن به مطلب

درخواست آموزش ساده ساخت متاباکس


پست های پیشنهاد شده

متاسفانه تمام آموزش های ساخت متاباکس که در وب فارسی وجود داره کپی برداری از هم دیگه هستش و دارای مشکل در هنگام اجرای کدها و گنگ بودن آموزشها

از دوستان کسی هست کدهای سالم اضافه کردن متاباکس به وردپرس برای فیلدهایی مانند زیر را قرار دهد

فیلد قیمت:


<?php $price = get_post_meta($post->ID, 'price', true); ?><?php if(get_post_custom_values('price')) : ?><?php echo ( $price ); ?> تومان<?php endif; ?>

فیلد لینک دانلود:

<?php $download = get_post_meta($post->ID, 'download', true); ?><?php if(get_post_custom_values('download')) : ?><a href="<?php echo $dlid; ?>">دانلود</a><?php endif; ?>

لینک به ارسال

متاسفانه تمام آموزش های ساخت متاباکس که در وب فارسی وجود داره کپی برداری از هم دیگه هستش و دارای مشکل در هنگام اجرای کدها و گنگ بودن آموزشها

از دوستان کسی هست کدهای سالم اضافه کردن متاباکس به وردپرس برای فیلدهایی مانند زیر را قرار دهد

فیلد قیمت:


<?php $price = get_post_meta($post->ID, 'price', true); ?><?php if(get_post_custom_values('price')) : ?><?php echo ( $price ); ?> تومان<?php endif; ?>

فیلد لینک دانلود:

<?php $download = get_post_meta($post->ID, 'download', true); ?><?php if(get_post_custom_values('download')) : ?><a href="<?php echo $dlid; ?>">دانلود</a><?php endif; ?>

توی مخزن وردپرس هست:


<?php
/**
* Adds a box to the main column on the Post and Page edit screens.
*/
function myplugin_add_meta_box() {
$screens = array( 'post', 'page' );
foreach ( $screens as $screen ) {
add_meta_box(
'myplugin_sectionid',
__( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_meta_box_callback',
$screen
);
}
}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );
/**
* Prints the box content.
*
* @param WP_Post $post The object for the current post/page.
*/
function myplugin_meta_box_callback( $post ) {
// Add a nonce field so we can check for it later.
wp_nonce_field( 'myplugin_save_meta_box_data', 'myplugin_meta_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$value = get_post_meta( $post->ID, '_my_meta_value_key', true );
echo '<label for="myplugin_new_field">';
_e( 'Description for this field', 'myplugin_textdomain' );
echo '</label> ';
echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="' . esc_attr( $value ) . '" size="25" />';
}
/**
* When the post is saved, saves our custom data.
*
* @param int $post_id The ID of the post being saved.
*/
function myplugin_save_meta_box_data( $post_id ) {
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonce'], 'myplugin_save_meta_box_data' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, it's safe for us to save the data now. */

// Make sure that it is set.
if ( ! isset( $_POST['myplugin_new_field'] ) ) {
return;
}
// Sanitize user input.
$my_data = sanitize_text_field( $_POST['myplugin_new_field'] );
// Update the meta field in the database.
update_post_meta( $post_id, '_my_meta_value_key', $my_data );
}
add_action( 'save_post', 'myplugin_save_meta_box_data' );


https://codex.wordpress.org/Function_Reference/add_meta_box

لینک به ارسال

همیشه و در همه حال به همه گفتم بهترین منبع برای یادگیری وردپرس خودش هست این بخش ها رو ببینید :

https://codex.wordpress.org/Custom_Fields

این برای دانلود :


<?php $download = get_post_meta($post->ID, 'download', true); ?>
<?php if($download) : ?>
<a href="<?php echo $download; ?>">دانلود</a>
<?php endif; ?>

این برای قیمت


<?php $price = get_post_meta($post->ID, 'price', true); ?>
<?php if($price) : ?>
<?php echo $price; ?> تومان
<?php endif; ?>

لینک به ارسال

همیشه و در همه حال به همه گفتم بهترین منبع برای یادگیری وردپرس خودش هست این بخش ها رو ببینید :

https://codex.wordpr...g/Custom_Fields

این برای دانلود :


<?php $download = get_post_meta($post->ID, 'download', true); ?>
<?php if($download) : ?>
<a href="<?php echo $download; ?>">دانلود</a>
<?php endif; ?>

این برای قیمت


<?php $price = get_post_meta($post->ID, 'price', true); ?>
<?php if($price) : ?>
<?php echo $price; ?> تومان
<?php endif; ?>

فکر کنم سوالشون رو درست متوجه نشدید.

لینک به ارسال

تشکر از دوستان

اگر امکان دارد بفرمایید برای اون دوتا فیلد چه کدهایی در فانکشن و چه کدهایی در قالب قرار دهم

من الان از زمینه دلخواه برای نمایش این فیلدها در سایت استفاده میکنم که میخواهم آنها را بصورت متاباکس در بیارم و در سایت نمایش بدم

ویرایش شده توسط ahmad4u
لینک به ارسال

توضیح فارسی توابع ساخت متاباکس طبق نمونه ای که جناب Ghaem معرفی کردند:


<?php
/**
* Adds a box to the main column on the Post and Page edit screens.
*/
function myplugin_add_meta_box() { //-> نام تابع اصلی ساخت متاباکس (نامی دلخواه و غیر تکراری)
$screens = array( 'post', 'page' );//-> معرفی پست تایپهایی که متاباکس به ویرایشگرشون اضافه می شه
foreach ( $screens as $screen ) {
add_meta_box(
'myplugin_sectionid',
__( 'My Post Section Title', 'myplugin_textdomain' ),//-> عنوان متاباکس بجای myplugin_textdomain باید نام تکست دامین خود را جایگزین کنید تا بتوانید عنوان را با فایل زبان ترجمه کنید
'myplugin_meta_box_callback',//-> تابع برگشتی متاباکس برای معرفی دستورات متاباکس
$screen
);
}
}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );//-> اکشن ساخت متاباکس
/**
* Prints the box content.
*
* @param WP_Post $post The object for the current post/page.
*/
function myplugin_meta_box_callback( $post ) { //-> تابع برگشتی این نام باید با نام تابع برگشتی در تابع اصلی یکسان باشد
// Add a nonce field so we can check for it later.
wp_nonce_field( 'myplugin_save_meta_box_data', 'myplugin_meta_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$value = get_post_meta( $post->ID, '_my_meta_value_key', true ); //-> فراخوانی نتیجه متاباکس نام _my_meta_value_key نام زمینه دلخواهیست که این متاباکس می سازد
echo '<label for="myplugin_new_field">';
_e( 'Description for this field', 'myplugin_textdomain' ); //-> توضیح متاباکس (بازهم myplugin_textdomain باید با تکست دامین شما جابجا شود.)
echo '</label> ';
echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="' . esc_attr( $value ) . '" size="25" />';//-> فیلد اصلی متاباکس در این نمونه متاباکس به صورت رشته تعذیف شده می توان متاباکس را به صورت آرایه تعریف نمود و بجای یک فیلد از چندین فیلد برای متغییر ها استفاده نمود
}
/**
* When the post is saved, saves our custom data.
*
* @param int $post_id The ID of the post being saved.
*/
function myplugin_save_meta_box_data( $post_id ) { //->تابع ذخیره سازی شامل چک نمودن فیلدهای لازم و دسترسی کاربر برای ذخیره و تابع به روز رسانی زمینه دلخواه ساخته شده با متاباکس
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonce'], 'myplugin_save_meta_box_data' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
if ( ! isset( $_POST['myplugin_new_field'] ) ) {
return;
}
// Sanitize user input.
$my_data = sanitize_text_field( $_POST['myplugin_new_field'] );
// Update the meta field in the database.
update_post_meta( $post_id, '_my_meta_value_key', $my_data );
}
add_action( 'save_post', 'myplugin_save_meta_box_data' );//-> اکشن اجرای تابع ذخیره سازی در هنگام ذخیره مطلب

این کدها را باید به فانکشنز اضافه کنید

و کد


$value = get_post_meta( $post->ID, '_my_meta_value_key', true );
echo $value;

را هم در محل مناسب در قالب برای نمایش نتیجه

لینک به ارسال

با تشکر از دستان

الان سه سوال؟

اگر بخواهم ورودی قیمت بصورتی باشد که بین هر 3عدد یک , قرار دهد بصورت 30,000 باید چیکار کنم؟

و دوم اینکه برای اضافه کردن یک فیلد دیکر (لینک دانلود) به متاباکس چه کدی اضافه کنم؟

و سوم اینکه میخواهم اگر فیلد در متاباکس تکمیل نشد آن فیلد در قالب نمایش ندهد و فقط فیلدهایی که تکمیل میشوند در قالب نمایش داده شوند

ویرایش شده توسط ahmad4u
لینک به ارسال


<?php
/**
* Adds a box to the main column on the Post and Page edit screens.
*/
function myplugin_add_meta_box() {
$screens = array( 'post', 'page' );
foreach ( $screens as $screen ) {
add_meta_box(
'myplugin_sectionid',
__( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_meta_box_callback',
$screen
);
}
}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );
/**
* Prints the box content.
*
* @param WP_Post $post The object for the current post/page.
*/
function myplugin_meta_box_callback( $post ) {
// Add a nonce field so we can check for it later.
wp_nonce_field( 'myplugin_save_meta_box_data', 'myplugin_meta_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$value = get_post_meta( $post->ID, '_my_meta_value_key', true );
$value1 = get_post_meta( $post->ID, 'download_url', true );
echo '<label for="myplugin_new_field">';
_e( 'Description for this field', 'myplugin_textdomain' );
echo '</label> ';
echo '<input type="text" dir="ltr" onblur="this.value=addComma(this.value)" onfocus="this.value=removeComma(this.value)" id="myplugin_new_field" name="myplugin_new_field" value="' . esc_attr( $value ) . '" size="25" />';
echo '<br>';
echo '<label for="myplugin_download_url">';
_e( 'Download url', 'myplugin_textdomain' );
echo '</label> ';
echo '<input type="url" dir="ltr" id="myplugin_download_url" name="myplugin_download_url" value="' . esc_attr( $value1 ) . '" size="25" />';?>
<script>
function addComma( str ) {
var objRegex = new RegExp( '(-?[0-9]+)([0-9]{3})' );
while( objRegex.test( str ) ) {
str = str.replace( objRegex, '$1,$2' );
}
return str;
}
function removeComma(str) {
str = str.replace(/,/gi, '');
return str;
}
</script>
<?php
}
/**
* When the post is saved, saves our custom data.
*
* @param int $post_id The ID of the post being saved.
*/
function myplugin_save_meta_box_data( $post_id ) {
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonce'], 'myplugin_save_meta_box_data' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
if ( ! isset( $_POST['myplugin_new_field'] ) || ! isset( $_POST['myplugin_download_url'] ) ) {
return;
}
// Sanitize user input.
$my_data = sanitize_text_field( $_POST['myplugin_new_field'] );
$my_data = str_replace(',', '', $my_data);
$my_data1 = sanitize_text_field( $_POST['myplugin_download_url'] );
// Update the meta field in the database.
update_post_meta( $post_id, '_my_meta_value_key', $my_data );
update_post_meta( $post_id, 'download_url', $my_data1 );
}
add_action( 'save_post', 'myplugin_save_meta_box_data' );
?>

و نمایش


$my_data = get_post_meta( $post->ID, '_my_meta_value_key' );
$my_data1 = get_post_meta( $post->ID, 'download_url');
if($my_data) {
echo number_format(get_post_meta( $post->ID, '_my_meta_value_key', true ), 0);
}
if($my_data1) {
echo get_post_meta( $post->ID, 'download_url', true );
}

لینک به ارسال

به گفتگو بپیوندید

هم اکنون می توانید مطلب خود را ارسال نمایید و بعداً ثبت نام کنید. اگر حساب کاربری دارید، برای ارسال با حساب کاربری خود اکنون وارد شوید .

مهمان
ارسال پاسخ به این موضوع ...

×   شما در حال چسباندن محتوایی با قالب بندی هستید.   حذف قالب بندی

  تنها استفاده از 75 اموجی مجاز می باشد.

×   لینک شما به صورت اتوماتیک جای گذاری شد.   نمایش به صورت لینک

×   محتوای قبلی شما بازگردانی شد.   پاک کردن محتوای ویرایشگر

×   شما مستقیما نمی توانید تصویر خود را قرار دهید. یا آن را اینجا بارگذاری کنید یا از یک URL قرار دهید.

×
×
  • اضافه کردن...