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';
}
?>