使用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个回答

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" ?

dqtl46964
dqtl46964 是的,这是一个错误,但它仍然无法解决我的问题
6 年多之前 回复
doudi2520
doudi2520 这似乎是你的代码中的一个错误,检查这一行:url:“<?php echo get_template_directory_uri();?> / ajax_get /”,它应该是url:“<?php echo get_template_directory_uri();?> / ajax_get .PHP”
6 年多之前 回复
douzhuanqian8244
douzhuanqian8244 谢谢你..我更新了我的问题,包括ajax_get.php文件的代码。 我真的很感激帮助指出我正确的方向
6 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问