doulingzou1712 2014-03-24 04:26 采纳率: 0%
浏览 50

使用jQuery在Wordpress中动态添加元键名称

I have jQuery function that dynamically adds the Meta Key Name and Meta Key Value in Wordpress when a a checkbox in a certain custom post type is clicked. Currently the function Stores the post Id as the Meta Key Name but I would like to change this to say checkbox_meta_key so all posts under this custom post type have this Meta Key Name.

(function($){
        $('li.todo').click(function(){
            if($(this).find('.uncheck_box').length >0){
                var _t=$(this).find('.uncheck_box');
                _t.removeClass('uncheck_box');
                _t.addClass('check_box');
                m_val='1';
                $(this).find('a').addClass('strike');
            }else{
                m_val='0';
                var _t=$(this).find('.check_box');
                _t.removeClass('check_box');
                _t.addClass('uncheck_box');
                $(this).find('a').removeClass('strike');
            }

            //trigger a custom event here
            $(this).trigger('updateclass')

        var m_key=jQuery(this).attr('id');
            jQuery.ajax({
              type: "POST",
              url: "<?php echo get_template_directory_uri(); ?  >/ajax_get.php",
              data: { meta_key: m_key, meta_value: m_val},
               beforeSend: function(   ) {
                 //jQuery(this).attr("disabled", true);       
              },
              success:function(){}
            })
        });

        var $checkboxes = jQuery('li.todo'),
        $completeCount = jQuery('.complete-count'),
        $incompleteCount = jQuery('.incomplete-count');

        var updateCount = function(){
        $completeCount.text(jQuery('.check_box').length);
        $incompleteCount.text(jQuery('.uncheck_box').length);
        };  

        $checkboxes.on('updateclass', updateCount);
        updateCount();



    $('.sort, .itemage').on('click', function (e) {
  var $this = $(this).hasClass('sort') ? $(this).addClass('active') : $('.sort.active');

  var itemage = $('.itemage').val(),
    b = itemage == 10;

  $('.sort').not($this).removeClass('active');

  if ($this.hasClass('showall')) {
    $('li.todo').hide().filter(function () {
        return (this.getAttribute('itemage') === itemage || b);
    }).show();
    return;
  }

  var sel = $this.hasClass('incomplete') ? 'span.uncheck_box' : 'span.check_box';

  $('li.todo').hide().filter(function () {
    return (this.getAttribute('itemage') === itemage || b) && !!   $(this).find(sel).length;
  }).show();

var count=$(".leftlist li:visible").length;
  document.cookie="incompletecount="+count;
 })
    })(jQuery)

    jQuery(document).ready(function(){ 
        jQuery('.chkbx').click(function(){

        var cuser_id='<?php echo $current_user->ID;?>';
        var m_val='0';
        if(jQuery(this).is(':checked'))
        {
            m_val='1';
            jQuery(this).parent().next("dd").addClass('strike');
        }else
        {
            m_val='0';
            jQuery(this).parent().next("dd").removeClass('strike');
        }
        var m_key=jQuery(this).attr('id');
            jQuery.ajax({
              type: "POST",
              url: "<?php echo get_template_directory_uri(); ?>/ajax_get/",
              data: { meta_key: m_key, meta_value: m_val},
               beforeSend: function(   ) {
                 jQuery(this).attr("disabled", true);         }
            })
              .done(function( msg ) {
              jQuery(this).attr("disabled", false);     
              });
        });
    });

Where the ajax_get.php has the following code:

<?php


 require_once('../../../wp-load.php' );
 wp();

 if (  is_user_logged_in() ) {
 $current_user = wp_get_current_user();
 update_user_meta($current_user->ID, $_REQUEST['meta_key'],$_REQUEST['meta_value']);
 echo $_REQUEST['meta_value'];
 }
    else{
echo'0';
 }
?>
  • 写回答

1条回答

  • dongyi1777 2014-03-24 07:37
    关注

    Basically you need to implement a database query, which will update all records, based on the new meta key, this can be done in your ajax_get.php file.

    This is how you can do it.

    Unfortunately your question is incomplete and I cannot give you more clues.

    EDIT:

    It seems to be an error in your code, check this line:

    url: "<?php echo get_template_directory_uri(); ?>/ajax_get/",

    should it be

    url: "<?php echo get_template_directory_uri(); ?>/ajax_get.php" ?

    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler