Những đoạn code hay dùng để code plugin wordpress

3 năm trước, Plugin, 334 Lượt xem

Tổng hợp những đoạn code hay dùng để code plugin wordpress

// This enables debugging.
define( 'WP_DEBUG', true );
// This disables debugging.
define( 'WP_DEBUG', false );

Kiểm tra array


echo '<pre>';
print_r($meta_element_class);
echo'</pre>';   

Chuyển đổi 1 chuỗi thành mảng và chuyển qua mảng javascript

$str = "xin chào các bạn,netweb hôm nay sẽ hướng dẫn,hihih ok ok";
// chuyển 1 chuỗi thành mảng sau dấu phẩy
$arr = explode(",",$str);

echo '<pre>';
print_r($arr);
echo'</pre>';  

// ở javascript
<script type="text/javascript">

var arCuMessages = <?php echo json_encode($arr); ?>;
//console.log(arCuMessages);
</script>

Chèn shortcode vào theme

<?php echo do_shortcode('[name_of_shortcode]'); ?>

Thêm 1 metabox custom post type

Lấy metabox ra ngoài

<?php echo esc_attr( get_post_meta( get_the_ID(), 'milage', true ) ); ?>

Save metabox dropdown show data ra ngoài

<?php echo get_post_meta($post->ID, 'custom_element_grid_class_meta_box', true); ?>

<?php

add_action( 'add_meta_boxes', 'tinh_thanh_custom_meta_box' );

function tinh_thanh_custom_meta_box($post){
    add_meta_box('so_meta_box', 'Custom Box', 'tinh_thanh_class_meta_box', 'bat_dong_san', 'normal' , 'high');
}

add_action('save_post', 'tinh_thanh_save_metabox');

function tinh_thanh_save_metabox(){ 
    global $post;
    if(isset($_POST["tinh_thanh_class"])){
         //UPDATE: 
        $meta_element_class = $_POST['tinh_thanh_class'];
        //END OF UPDATE

        update_post_meta($post->ID, 'tinh_thanh_class_meta_box', $meta_element_class);
        //print_r($_POST);
    }
}




function tinh_thanh_class_meta_box($post){
    $tinhthanh = get_all_cities();

    $meta_element_class = get_post_meta($post->ID, 'tinh_thanh_class_meta_box', true); //true ensures you get just one value instead of an array

    echo $meta_element_class;

    // echo '<pre>';
    // print_r($meta_element_class);
    // echo'</pre>';   

    ?>   
    <label>Choose the size of the element :  </label>

    aaaaaa

    <select name="tinh_thanh_class" id="tinh_thanh_class">
    <?php
        foreach ($tinhthanh as $key => $value) {
        ?>
            <option value="<?php echo $value; ?>" <?php selected( $meta_element_class, $value); ?>><?php echo $value; ?></option>
        <?php  } ?>
    </select>
    


    <?php
}


?>
<?php echo esc_attr( get_post_meta( get_the_ID(), 'tinh_thanh_class_meta_box', true ) ); ?>

lấy giá trị từ input gán selected cho option select

//gán giá trị selected cho select lấy từ 1 input
            var tinhTPValue = jQuery("#tinhTPValue").val();
            jQuery("#tinh-thanh").val(tinhTPValue)
                .find("option[value=" + tinhTPValue + "]").attr('selected', true);

Bài viết mới cập nhập

  • Hướng dẫn Plugin floating contact

    Hướng dẫn Plugin floating contact

    10 tháng trước, 336 Lượt xem

    Netweb xin hướng dẫn cách quản lý Plugin floating contact các bạn nhớ làm theo kỹ tường bước để plugin hoạt động Tính năng: Hiển…

  • Backup Yandex Mail

    Backup Yandex Mail

    12 tháng trước, 225 Lượt xem

    Khác với dịch vụ email theo tên của microsoft và google với chi phí đắt đỏ tầm 1tr/năm/1user, vì thế nên netweb khuyên dùng dịch…

Bình luận trên facebook netweb.vn

Đam mê thiết kế web, kinh nghiệm 9 năm trong nghề thiết kế website, đang thiết kế web dạo tại netweb.vn, Callme: 08.9898.2526 (Mr Hải)