How can I set the max button click limit? - php
I'm trying to set max limit.When there are any 2 button pressed there must be a action (the other buttons get disabled).
This is code of the post reaction in wordpress.
I tried first something like at this link
.http://jsfiddle.net/r6KYu/ But I could not.How can I counting the clicks and set max click limit ?
(function($) {
$(document).ready(function() {
$('#zuzu_viral_reactions li').click(function() {
var unreact = ($(this).hasClass("clicked") ? true : false);
var id = $(this).parent().data("postId") || $(this).parent().data("post-id");
var url = zvr_data.ajax_url;
var reaction = $(this).data().reaction;
$.post(url, { postid: id, action: 'zvr_react', reaction: reaction, unreact: unreact }, function(data) {
console.log("Ajax: " + data);
});
$(this).toggleClass("clicked");
var howMany = parseInt($(this).find('span').text());
if (howMany > 0) {
if ($(this).hasClass("clicked")) {
howMany += 1;
} else {
howMany -= 1;
}
} else {
howMany = 1;
}
$(this).find('span').text(howMany);
});
});
})(jQuery);
document.addEventListener("touchstart", function() {}, true);
if ('createTouch' in document) {
try {
var ignore = /:hover/;
for (var i = 0; i < document.styleSheets.length; i++) {
var sheet = document.styleSheets[i];
for (var j = sheet.cssRules.length - 1; j >= 0; j--) {
var rule = sheet.cssRules[j];
if (rule.type === CSSRule.STYLE_RULE && ignore.test(rule.selectorText)) {
sheet.deleteRule(j);
}
}
}
} catch (e) {}
}
Also php file here.
<?php
/*
Plugin Name: Zuzu Viral Reactions
Plugin URI: http://tiguandesign.com/
Description: Simple WordPress reactions plugin for viral stories.
Author: Tiguan
Author URI: http://tiguandesign.com/
Licence: GPLv2
Version: 1.0
Stable Tag: 1.0
*/
class Zuzu_Viral_React {
// $reactions = array( 'like','love', 'win', 'cute', 'lol', 'omg', 'wtf', 'fail' );
function __construct() {
$this->defaults = array(
'like' => "Like",
'love' => "LOVE",
'win' => "Win",
'cute' => "Cute",
'lol' => "LOL",
'omg' => "OMG",
'wtf' => "WTF",
'fail' => "Fail",
'boxtitle' => "Your Reaction"
);
add_action('the_content', array($this,'addContent'));
add_action('the_excerpt', array($this, 'zvrdisablePlugin'));
add_action('admin_menu', array($this, 'addMenu'));
add_action( 'admin_init', array($this, 'registerSettings'));
add_action( 'wp_ajax_zvr_react', array($this,'react'));
add_action( 'wp_ajax_nopriv_zvr_react', array($this,'react' ));
add_action('wp_enqueue_scripts', array($this,'addStylesAndScripts'));
add_action( 'load-post.php', array($this, 'initMetaBox'));
add_action( 'load-post-new.php', array($this, 'initMetaBox'));
add_shortcode( 'zvr_reactions', array($this, 'shortCode') );
add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array($this, 'addSettingsLink' ));
}
function addSettingsLink ( $links ) {
$link = array('Settings');
return array_merge( $links, $link );
}
function initMetaBox() {
add_action( 'add_meta_boxes', array($this, 'addMetaBox'));
add_action( 'save_post', array($this, 'savePostMeta'), 10, 2 );
}
function savePostMeta($post_id, $post) {
if ( !isset( $_POST['zvr_enable_meta_nonce'] ) || !wp_verify_nonce( $_POST['zvr_enable_meta_nonce'], basename( __FILE__ ) ) )
return $post_id;
$post_type = get_post_type_object( $post->post_type );
if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
return $post_id;
$meta_value = ( isset( $_POST['zvr_enable'] ) ? sanitize_html_class( $_POST['zvr_enable'] ) : '' );
if (empty($meta_value)) {
$meta_value = "off";
}
update_post_meta( $post_id, 'zvr_enable', $meta_value );
}
function addMetaBox() {
add_meta_box('zvr-enable-on-post', 'Zuzu Viral Reactions', array($this, 'renderMetaBox'), 'post', 'normal', 'default');
}
function renderMetaBox() {
$options = get_option( 'zvr_settings' );
$enable = isset($options['zvr_auto_enable']) ? $options['zvr_auto_enable']: 'on';
$post_id = get_the_ID();
$meta_enable = get_post_meta( $post_id, 'zvr_enable', true );
if (!empty($meta_enable)) {
$enable = $meta_enable;
}
wp_nonce_field( basename( __FILE__ ), 'zvr_enable_meta_nonce' );
?>
<label><input type="checkbox" name="zvr_enable" id="zvr-enable" <?php checked($enable, 'on')?>>Enable reactions on this post</label>
<?php
}
function addMenu() {
add_options_page('Zuzu Reaction Settings', 'Zuzu Reaction', 'manage_options', 'zvr_options', array($this, 'renderOptionsPage'));
}
function registerSettings() {
register_setting('zvr_options', 'zvr_settings');
add_settings_section( 'zvr_enable', '', array($this, 'renderEnableGuide'), 'zvr_options' );
add_settings_field( 'zvr_options-auto-enable-on', 'Show buttons on posts', array($this, 'renderRadio'), 'zvr_options', 'zvr_enable', array('value' => 'on'));
add_settings_field( 'zvr_options-auto-enable-off', "Don't show buttons on posts", array($this, 'renderRadio'), 'zvr_options', 'zvr_enable', array('value' => 'off'));
add_settings_section( 'zvr_content', '', array($this, 'renderContent'), 'zvr_options' );
add_settings_section( 'zvr_share_translations', 'Reaction box title', array($this, 'renderReactionTranslations'), 'zvr_options');
add_settings_field( 'zvr_options-boxtitle', 'Your Reaction', array($this, 'renderField'), 'zvr_options', 'zvr_share_translations', array('label' => 'boxtitle'));
add_settings_section( 'zvr_translations', 'Reactions Text', array($this, 'renderReactionTranslations'), 'zvr_options' );
add_settings_field( 'zvr_options-like', 'Like', array($this, 'renderField'), 'zvr_options', 'zvr_translations', array('label' => 'like'));
add_settings_field( 'zvr_options-love', 'Love', array($this, 'renderField'), 'zvr_options', 'zvr_translations', array('label' => 'love'));
add_settings_field( 'zvr_options-win', 'Win', array($this, 'renderField'), 'zvr_options', 'zvr_translations', array('label' => 'win'));
add_settings_field( 'zvr_options-cute', 'Cute', array($this, 'renderField'), 'zvr_options', 'zvr_translations', array('label' => 'cute'));
add_settings_field( 'zvr_options-lol', 'LOL', array($this, 'renderField'), 'zvr_options', 'zvr_translations', array('label' => 'lol'));
add_settings_field( 'zvr_options-omg', 'OMG', array($this, 'renderField'), 'zvr_options', 'zvr_translations', array('label' => 'omg'));
add_settings_field( 'zvr_options-wtf', 'WTF', array($this, 'renderField'), 'zvr_options', 'zvr_translations', array('label' => 'wtf'));
add_settings_field( 'zvr_options-fail', 'Fail', array($this, 'renderField'), 'zvr_options', 'zvr_translations', array('label' => 'fail'));
}
function shortCode() {
$options = get_option('zvr_settings');
return $this->renderPlugin($options);
}
function renderField($args) {
$label = $args['label'];
$options = get_option('zvr_settings');
$value = isset($options['zvr_'.$label]) ? $options['zvr_'.$label]: $this->defaults[$label];
echo "<input type='text' name='zvr_settings[zvr_$label]' value='".esc_attr($value)."'>";
}
function renderContent() {
?>
<div style="border-top: 1px solid #bbb; width: 100%; padding: 30px 0; margin: 30px 0; border-bottom: 1px solid #bbb;">
<h3>Adding reactions manually (short code)</h3>
<ol>
<li>You can use shortcode <code>[zvr_reactions]</code> within post or page text.</li>
<li>You can add <code>if (function_exists('zvr_reactions')) { zvr_reactions() }</code> into your templates.</li>
</ol>
</div>
<?php
}
function zvrdisablePlugin($excerpt) {
$pattern = '/zvr.*/i';
return preg_replace($pattern, '', $excerpt);
}
function renderRadio($args) {
$options = get_option( 'zvr_settings' );
$value = $args['value'];
$set_value = isset($options['zvr_auto_enable']) ? $options['zvr_auto_enable']: 'on';
?>
<input type='radio' name='zvr_settings[zvr_auto_enable]' <?php checked( $set_value, $value ); ?> value='<?php echo $value ?>'>
<?php
}
function renderReactionTranslations() {
echo "";
}
function renderEnableGuide() {
?>
<?php
}
function renderOptionsPage() {
?>
<form action='options.php' method='post'>
<p><h1>Zuzu Viral Reaction Settings</h1></p><br />
<p>Select the default setting for Zuzu Viral Reaction visibility. You can override this setting for each post in the post editor.</p>
<?php
settings_fields( 'zvr_options' );
do_settings_sections( 'zvr_options' );
?>
<?php submit_button(); ?>
</form>
<?php
}
function addContent($content) {
$options = get_option('zvr_settings');
$show_on_every_post = isset($options['zvr_auto_enable']) ? $options['zvr_auto_enable'] : 'on';
$post_id = get_the_ID();
$enabled = get_post_meta( $post_id, 'zvr_enable', true );
if (!is_page() && ($enabled=="on" || (empty($enabled) && $show_on_every_post=='on'))) {
$plugin = $this->renderPlugin($options);
$content .= $plugin;
}
return $content;
}
function renderPlugin($options) {
$post_id = get_the_ID();
$post_url = get_permalink($post_id);
$label_like =isset($options['zvr_like']) ? $options['zvr_like']: $this->defaults['like'];
$label_love =isset($options['zvr_love']) ? $options['zvr_love']: $this->defaults['love'];
$label_win =isset($options['zvr_win']) ? $options['zvr_win']: $this->defaults['win'];
$label_cute =isset($options['zvr_cute']) ? $options['zvr_cute']: $this->defaults['cute'];
$label_lol =isset($options['zvr_lol']) ? $options['zvr_lol']: $this->defaults['lol'];
$label_omg =isset($options['zvr_omg']) ? $options['zvr_omg']: $this->defaults['omg'];
$label_wtf =isset($options['zvr_wtf']) ? $options['zvr_wtf']: $this->defaults['wtf'];
$label_fail =isset($options['zvr_fail']) ? $options['zvr_fail']: $this->defaults['fail'];
$label_boxtitle =isset($options['zvr_boxtitle']) ? $options['zvr_boxtitle']: $this->defaults['boxtitle'];
ob_start() ?>
<div id="zuzu_viral_reactions">
<span style="display:none">zvr</span>
<div class="zvr-reaction-title"><?php echo $label_boxtitle ?></div>
<ul data-post-id="<?php echo $post_id ?>">
<li class="animated" data-reaction="like" <?php echo $this->getClass("like", $post_id) ?> ><img class="animated" src="<?php echo trailingslashit( plugin_dir_url( __FILE__ ) ) . 'assets/img/1f44d.svg' ?>" /><em><?php echo $label_like ?></em><span><?php echo $this->getAmount("like",$post_id) ?></span></li>
<li class="animated" data-reaction="love" <?php echo $this->getClass("love", $post_id) ?> ><img class="animated" src="<?php echo trailingslashit( plugin_dir_url( __FILE__ ) ) . 'assets/img/1f60d.svg' ?>" /><em><?php echo $label_love ?></em><span><?php echo $this->getAmount("love",$post_id) ?></span></li>
<li class="animated" data-reaction="win" <?php echo $this->getClass("win", $post_id) ?> ><img class="animated" src="<?php echo trailingslashit( plugin_dir_url( __FILE__ ) ) . 'assets/img/1f61c.svg' ?>" /><em><?php echo $label_win ?></em><span><?php echo $this->getAmount("win",$post_id) ?></span></li>
<li class="animated" data-reaction="cute" <?php echo $this->getClass("cute", $post_id) ?> ><img class="animated" src="<?php echo trailingslashit( plugin_dir_url( __FILE__ ) ) . 'assets/img/1f917.svg' ?>" /><em><?php echo $label_cute ?></em><span><?php echo $this->getAmount("cute",$post_id) ?></span></li>
<li class="animated" data-reaction="lol" <?php echo $this->getClass("lol", $post_id) ?> ><img class="animated" src="<?php echo trailingslashit( plugin_dir_url( __FILE__ ) ) . 'assets/img/1f632.svg' ?>" /><em><?php echo $label_lol ?></em><span><?php echo $this->getAmount("lol",$post_id) ?></span></li>
<li class="animated" data-reaction="omg" <?php echo $this->getClass("omg", $post_id) ?> ><img class="animated" src="<?php echo trailingslashit( plugin_dir_url( __FILE__ ) ) . 'assets/img/1f631.svg' ?>" /><em><?php echo $label_omg ?></em><span><?php echo $this->getAmount("omg",$post_id) ?></span></li>
<li class="animated" data-reaction="wtf" <?php echo $this->getClass("wtf", $post_id) ?> ><img class="animated" src="<?php echo trailingslashit( plugin_dir_url( __FILE__ ) ) . 'assets/img/1f914.svg' ?>" /><em><?php echo $label_wtf ?></em><span><?php echo $this->getAmount("wtf",$post_id) ?></span></li>
<li class="animated" data-reaction="fail" <?php echo $this->getClass("fail", $post_id) ?> ><img class="animated" src="<?php echo trailingslashit( plugin_dir_url( __FILE__ ) ) . 'assets/img/1f915.svg' ?>" /><em><?php echo $label_fail ?></em><span><?php echo $this->getAmount("fail",$post_id) ?></span></li>
</ul>
<div style="clear: both;"></div>
</div>
<?php
$plugin = ob_get_contents();
ob_clean();
return $plugin;
}
function getClass($reaction, $post_id) {
$clicked = isset($_COOKIE["zvr_reacted_".$reaction."_".$post_id]);
return ($clicked ? 'class="clicked"':'');
}
function getAmount($reaction, $post_id) {
$meta_key = "zvr_reaction_".$reaction;
$amount = get_post_meta($post_id, $meta_key, true) ? get_post_meta($post_id, $meta_key, true) : 0;
return $amount;
}
function react() {
if (isset($_POST["postid"])) {
$post_id = $_POST["postid"];
$reaction = $_POST["reaction"];
$unreact = $_POST["unreact"];
}
$amount = $this->getAmount($reaction, $post_id);
if (isset($unreact) && $unreact === "true") {
unset($_COOKIE['zvr_reacted_'.$reaction.'_'.$post_id]);
setcookie('zvr_reacted_'.$reaction.'_'.$post_id, '', time() - 3600, "/");
$amount = (int) $amount - 1;
if ($amount >=0) {
echo "Amount: ".$amount." ";
update_post_meta($post_id, "zvr_reaction_".$reaction, $amount);
}
}
else {
setcookie('zvr_reacted_'.$reaction.'_'.$post_id, $reaction, time() + (86400 * 30), "/");
$amount = (int) $amount + 1;
if ($amount >=0) {
echo "Amount: ".$amount." ";
update_post_meta($post_id, "zvr_reaction_".$reaction, $amount);
}
}
return;
}
function addStylesAndScripts() {
wp_enqueue_style( 'zvr-font', 'https://fonts.googleapis.com/css?family=Open+Sans' );
wp_enqueue_style( 'zvr-style', trailingslashit( plugin_dir_url( __FILE__ ) ) . 'assets/css/zvr-styles.css', array(), "1.0.3" );
wp_enqueue_script( 'zvr-script', trailingslashit( plugin_dir_url( __FILE__ ) ) . 'assets/js/zvr-script.js', array( 'jquery' ), "1.0.3" );
$localize = array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
);
wp_localize_script( 'zvr-script', 'zvr_data', $localize );
}
}
function zvr_reactions() {
// Call from templates
// if (function_exists('zvr_reactions')) { zvr_reactions() }
$zvr = new Zuzu_Viral_React();
$options = get_option('zvr_settings');
echo $zvr->renderPlugin($options);
}
new Zuzu_Viral_React();
you can solve this by jquery and get max length event by the following code
<label for="name">Input 1: maxlength=10</label>
<input type="text" maxlength="10" id="input-1" value="Allen"/>
$("#input-1").maxlength();
// get maxlength and other event
$("input").bind("update.maxlength", function(event, element, lastLength, length, maxLength, left){
console.log(event, element, lastLength, length, maxLength, left);
});
Related
How to add custom field for upload PDF files in Woocommerce product in admin
If I need to add simply text input, I can use a function like woocommerce_wp_text_input inside (for eg.) woocommerce_product_options_advanced. Then I use woocommerce_process_product_meta where I can use update_post_meta. This is clear and handy! Now - I would like to add a custom field with the PDF upload option as I would like to attach PDF files to the product. So is it possible to add such a field in a similar way? Note: This is NOT a virtual download product. It's just simple product.
You can add a custom meta boxes to upload a pdf. You can retrieve it by using <?php get_post_meta( get_the_ID(), 'advanced_options_pdf', true ); ?> on the front end. Just paste the following on your function.php file. class Advanced_Options { private $config = '{"title":"Advanced Options","prefix":"advanced_options_","domain":"advanced-options","class_name":"Advanced_Options","post-type":["product"],"context":"normal","priority":"default","fields":[{"type":"media","label":"PDF","return":"url","id":"advanced_options_pdf"}]}'; public function __construct() { $this->config = json_decode( $this->config, true ); add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] ); add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); add_action( 'admin_head', [ $this, 'admin_head' ] ); add_action( 'save_post', [ $this, 'save_post' ] ); } public function add_meta_boxes() { foreach ( $this->config['post-type'] as $screen ) { add_meta_box( sanitize_title( $this->config['title'] ), $this->config['title'], [ $this, 'add_meta_box_callback' ], $screen, $this->config['context'], $this->config['priority'] ); } } public function admin_enqueue_scripts() { global $typenow; if ( in_array( $typenow, $this->config['post-type'] ) ) { wp_enqueue_media(); } } public function admin_head() { global $typenow; if ( in_array( $typenow, $this->config['post-type'] ) ) { ?><script> jQuery.noConflict(); (function($) { $(function() { $('body').on('click', '.rwp-media-toggle', function(e) { e.preventDefault(); let button = $(this); let rwpMediaUploader = null; rwpMediaUploader = wp.media({ title: button.data('modal-title'), button: { text: button.data('modal-button') }, multiple: true }).on('select', function() { let attachment = rwpMediaUploader.state().get('selection').first().toJSON(); button.prev().val(attachment[button.data('return')]); }).open(); }); }); })(jQuery); </script><?php } } public function save_post( $post_id ) { foreach ( $this->config['fields'] as $field ) { switch ( $field['type'] ) { default: if ( isset( $_POST[ $field['id'] ] ) ) { $sanitized = sanitize_text_field( $_POST[ $field['id'] ] ); update_post_meta( $post_id, $field['id'], $sanitized ); } } } } public function add_meta_box_callback() { $this->fields_table(); } private function fields_table() { ?><table class="form-table" role="presentation"> <tbody><?php foreach ( $this->config['fields'] as $field ) { ?><tr> <th scope="row"><?php $this->label( $field ); ?></th> <td><?php $this->field( $field ); ?></td> </tr><?php } ?></tbody> </table><?php } private function label( $field ) { switch ( $field['type'] ) { case 'media': printf( '<label class="" for="%s_button">%s</label>', $field['id'], $field['label'] ); break; default: printf( '<label class="" for="%s">%s</label>', $field['id'], $field['label'] ); } } private function field( $field ) { switch ( $field['type'] ) { case 'media': $this->input( $field ); $this->media_button( $field ); break; default: $this->input( $field ); } } private function input( $field ) { if ( $field['type'] === 'media' ) { $field['type'] = 'text'; } printf( '<input class="regular-text %s" id="%s" name="%s" %s type="%s" value="%s">', isset( $field['class'] ) ? $field['class'] : '', $field['id'], $field['id'], isset( $field['pattern'] ) ? "pattern='{$field['pattern']}'" : '', $field['type'], $this->value( $field ) ); } private function media_button( $field ) { printf( ' <button class="button rwp-media-toggle" data-modal-button="%s" data-modal-title="%s" data-return="%s" id="%s_button" name="%s_button" type="button">%s</button>', isset( $field['modal-button'] ) ? $field['modal-button'] : __( 'Select this file', 'advanced-options' ), isset( $field['modal-title'] ) ? $field['modal-title'] : __( 'Choose a file', 'advanced-options' ), $field['return'], $field['id'], $field['id'], isset( $field['button-text'] ) ? $field['button-text'] : __( 'Upload', 'advanced-options' ) ); } private function value( $field ) { global $post; if ( metadata_exists( 'post', $post->ID, $field['id'] ) ) { $value = get_post_meta( $post->ID, $field['id'], true ); } else if ( isset( $field['default'] ) ) { $value = $field['default']; } else { return ''; } return str_replace( '\u0027', "'", $value ); } } new Advanced_Options;
Wordpress plugin - options page not found
I was coding my very first plugin in order to share the posts on Facebook and Instagram, and I was writing the plugin options page. I get always the error "Options page not found". I thought that register_setting in the callback function could make the trick, but it didn't. What am I doing wrong? Here my code: <?php class Socialize { public function __construct() { // Hook into the admin menu add_action( 'admin_menu', array( $this, 'create_plugin_settings_page' ) ); add_action( 'admin_init', array( $this, 'setup_sections' ) ); add_action( 'admin_init', array( $this, 'setup_fields' ) ); } public function setup_fields() { add_settings_field( 'facebook_account', 'Facebook Username', array( $this, 'fb_account_callback' ), 'smashing_fields', 'facebook_section' ); add_settings_field( 'facebook_password', 'Facebook Password', array( $this, 'fb_pwd_callback' ), 'smashing_fields', 'facebook_section' ); add_settings_field( 'instagram_account', 'Instagram Username', array( $this, 'insta_account_callback' ), 'smashing_fields', 'instagram_section' ); add_settings_field( 'instagram_password', 'Instagram Password', array( $this, 'insta_pwd_callback' ), 'smashing_fields', 'instagram_section' ); } public function setup_sections() { add_settings_section( 'facebook_section', 'Facebook Account', array( $this, 'section_callback' ), 'smashing_fields' ); add_settings_section( 'instagram_section', 'Instagram Account', array( $this, 'section_callback' ), 'smashing_fields' ); } public function fb_account_callback( $arguments ) { echo '<input name="fb_account" id="fb_account" type="text" value="' . get_option( 'facebook_account' ) . '" />'; register_setting( 'smashing_fields', 'facebook_account' ); } public function fb_pwd_callback( $arguments ) { echo '<input name="fb_pwd" id="fb_pwd" type="password" value="' . get_option( 'facebook_password' ) . '" />'; register_setting( 'smashing_fields', 'facebook_password' ); } public function insta_account_callback( $arguments ) { echo '<input name="insta_account" id="insta_account" type="text" value="' . get_option( 'instagram_account' ) . '" />'; register_setting( 'smashing_fields', 'instagram_account' ); } public function insta_pwd_callback( $arguments ) { echo '<input name="insta_pwd" id="insta_pwd" type="password" value="' . get_option( 'instagram_password' ) . '" />'; register_setting( 'smashing_fields', 'instagram_password' ); } public function section_callback( $arguments ) { switch ( $arguments['id'] ) { case 'facebook_section': echo 'This is the Facebook section'; break; case 'instagram_section': echo 'This is the Instagram section'; break; } } public function create_plugin_settings_page() { // Add the menu item and page $page_title = 'Free Socialize'; $menu_title = 'Free Socialize'; $capability = 'manage_options'; $slug = 'smashing_fields'; $callback = array( $this, 'plugin_settings_page_content' ); $icon = 'dashicons-admin-plugins'; $position = 100; add_menu_page( $page_title, $menu_title, $capability, $slug, $callback, $icon, $position ); } public function plugin_settings_page_content() { ?> <div class="wrap"> <h2>Free Socialize Settings Page</h2> <form method="post" action="options.php"> <?php settings_fields( 'smashing_fields' ); do_settings_sections( 'smashing_fields' ); submit_button(); ?> </form> </div> <?php } } new Socialize(); ?>
I can't comment to gain clarification so apologies if I've misunderstood but would you not need to use add_plugins_page() in this instance? <?php add_plugins_page( $page_title, $menu_title, $capability, $menu_slug, $function); ?> Docs: https://codex.wordpress.org/Function_Reference/add_plugins_page Hope that helps and I've understood :)
How To Show Chechbox Result From Metabox To Single Post Wordpress?
Ask to everyone, i have problem. Here i try to use multiple chechbox to my custom post metabox. <?php function prodetail() { add_meta_box('pro_metabox', 'Detail Property', 'pro_metabox', 'property', 'normal', 'default'); } function pro_metabox() { global $post; echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />'; $postmeta = maybe_unserialize( get_post_meta( $post->ID, 'elements', true ) ); $elements = array( 'pool' => 'Pool', 'garage' => 'Garage', 'balcon' => 'Balcon', 'yard' => 'Yard', 'internet' => 'Internet' ); foreach ( $elements as $id => $element) { if ( is_array( $postmeta ) && in_array( $id, $postmeta ) ) { $checked = 'checked="checked"'; } else { $checked = null; } ?> <div class="pro-inn"> <div class="procols"> <div class="pro-inn"> <input type="checkbox" name="multval[]" value="<?php echo $id; ?>" <?php echo $checked; ?> /> <?php echo $element;?> </div> </div> </div> <?php } } function pro_meta($post_id, $post) { if ( !wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) )) { return $post->ID; } if ( !current_user_can( 'edit_post', $post->ID )) return $post->ID; if ( ! empty( $_POST['multval'] ) ) { update_post_meta( $post_id, 'elements', $_POST['multval'] ); } else { delete_post_meta( $post_id, 'elements' ); } } add_action('save_post', 'pro_meta', 1, 2); ?> help me to add code to show this checked result to single.php because my code use foreach just show Array text not show text like Pool Garage Balcon ect. Thanks
Use this code in your single.php file for your custom post $meta_value = get_post_meta( $post->ID, 'elements', true ); foreach($meta_value as $key=>$value){ echo $value . ' '; } It will show results same as you mentioned in the question ie: (Pool Garage Balcon ect.)
Notepad sticky plugin
I have this following code to give a textarea to a front end user to use it as notes on a page to write things to remember. But this code saves only one sticky note that can be edited whenever the user wants. My questions is if there is a way to save the content somewhere and have a new one every time submit is pressed. or a add new sticky button. require_once( plugin_dir_path( __FILE__ ) . 'wp-ajax.php' ); class notepad_stikey extends WP_Ajax { var $user; var $username; function __construct() { parent::__construct(); add_action('init', array( &$this, 'setup') ); } function setup() { $this->user = get_current_user_id(); $this->username = get_userdata( $this->user )->user_login; $this->notes = get_user_meta( $this->user, 'notepad_stikey', true ); $this->ph = ' '; //placeholder if (empty( $this->notes )) { $this->notes = $this->ph; } add_action('wp_enqueue_scripts', array( &$this, 'scripts') ); } function scripts() { wp_enqueue_script( 'notepad_stikey', plugins_url( 'notepad_stikey.js', __FILE__ ), array( 'jquery' ) ); wp_localize_script( 'notepad_stikey', 'notepad_stikey', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); } function an_change_notepad_stikey() { $notes = trim( $_POST['notes'] ); //if notes is empty, delete if ( empty($notes) ) { if ( delete_user_meta( $this->user, 'notepad_stikey' ) ) die( 'notes deleted' ); } //notes are the same, notes = placeholder, or resaving empty notes if ( $notes == $this->notes || $notes == $this->ph || ( empty($notes) && $this->notes == $this->ph) ) die(); //update if ( update_user_meta( $this->user, 'notepad_stikey', $notes ) ) die( 'updated' ); //hopefully, we don't get this far. if we do, something is wrong die( 'uh oh. notes could not be saved' ); } } global $notepad_stikey; $notepad_stikey = new notepad_stikey(); add_action( 'widgets_init', 'notepad_stikey_load' ); function notepad_stikey_load() { register_widget( 'notepad_stikey_Widget' ); } class notepad_stikey_Widget extends WP_Widget { function notepad_stikey_Widget() { $widget_ops = array('classname' => 'notepad_stikey', 'description' => __( 'notepad_stikey. Only one instance please. Else this will break.', 'notepad_stikey' ) ); $control_ops = array( 'id_base' => 'notepad_stikey' ); parent::WP_Widget( 'notepad_stikey', __( 'notepad_stikey', 'notepad_stikey' ), $widget_ops, $control_ops ); } function widget( $args, $instance ) { extract( $args, EXTR_SKIP ); echo $before_widget; global $notepad_stikey; $username = $notepad_stikey->username; $notes = $notepad_stikey->notes; //overwrite title $instance['title'] = 'Notepad for '. $username; echo $instance['hide_title'] ? '' : $before_title . $instance['title'] . $after_title; echo "<div id='notepad_stikey' class='$username' style='border: 1px solid #eee; padding: 10px 15px;min-height: 100px;'>"; echo $notes; echo '</div><span style="float:left;color:#008;" id="notepad_stikey_response"></span><small style="float:right;">click box above to edit</small>'; echo $after_widget; } //end widget() function update($new_instance, $old_instance) { $instance = $old_instance; $instance['title'] = esc_attr( $new_instance['title'] ); $instance['hide_title'] = (bool) $new_instance['hide_title'] ? 1 : 0; return $instance; } //end update() function form( $instance ) { $instance = wp_parse_args( (array) $instance, array( 'hide_title' => 0 ) ); extract( $instance ); ?> <p> <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hide_title'); ?>" name="<?php echo $this->get_field_name('hide_title'); ?>"<?php checked( $hide_title ); ?> /> <label for="<?php echo $this->get_field_id('hide_title'); ?>"><?php _e('Hide Title?', 'notepad_stikey' );?></label> </p> <?php } //end form() } if (!class_exists('WP_Ajax')) { class WP_Ajax { function __construct( $ajax_prefix = 'a', $nopriv_prefix = 'n' ) { $regex = "/^($ajax_prefix)?($nopriv_prefix)?_|^($nopriv_prefix)? ($ajax_prefix)?_/"; $methods = get_class_methods( $this ); foreach ( $methods as $method ) { if ( preg_match( $regex, $method, $matches ) ) { if ( count( $matches ) > 1 ) { $action = preg_replace( $regex, '', $method ); if ( count( $matches ) == 3 ) { add_action( "wp_ajax_$action", array( $this, $method ) ); add_action( "wp_ajax_nopriv_$action", array( $this, $method ) ); } else { if ( $matches[1] == $ajax_prefix ) { add_action( "wp_ajax_$action", array( $this, $method ) ); } else { add_action( "wp_ajax_nopriv_$action", array( $this, $method ) ); } } } } } } } } jQuery(document).ready(function($) { $('#notepad_stikey').click(function(e) { tag = e.target.tagName; if ( tag != 'TEXTAREA' && tag != 'INPUT' ) { contents = $(this).html(); $(this).html( '<textarea rows="5" cols="50" style="display:block;width:98%;height:100px;">' + contents + '</textarea><input type="submit" class="save" style="position:relative;z-index:99" />' ); } }); $('#notepad_stikey input.save').live( 'click', function() { new_contents = $(this).siblings('textarea').val(); $('#notepad_stikey').html( new_contents ); change_notepad_stikey( new_contents ); return false; }); function change_notepad_stikey( notes ) { $('#notepad_stikey_response').text( '...' ); $.post(notepad_stikey.ajaxurl, { 'action' : 'change_notepad_stikey', 'notes' : notes }, function(response) { //if (response != '') { //alert( response ); $('#notepad_stikey_response').text( response ); //} }, 'text' ); } });
get the modified date of post meta box
i'm searching for a way to display the modified date of a custom field / meta box. until now, i only know how to echo the_modified_date('l, j.n.Y'); but this one only works with the entire post. i'am using a meta_box for additional pdf files uploading, code: <?php class PDF_Metabox_id1 { function __construct() { add_action( 'post_mime_types', array( $this, 'pdf_mime_type_id1' ) ); add_action( 'add_meta_boxes', array( $this, 'admin_scripts_id1' ), 5 ); add_action( 'add_meta_boxes', array( $this, 'metabox_add_id1' ) ); add_action( 'save_post', array( $this, 'pdf_save_postdata_id1') ); add_action( 'wp_ajax_refresh_pdf', array( $this, 'refresh_pdf_id1' ) ); } function pdf_mime_type_id1() { $post_mime_types['application/pdf'] = array( __( 'PDFs' ), __( 'Manage PDFs' ), _n_noop( 'PDF <span class="count">(%s)</span>', 'PDFs <span class="count">(%s)</span>' ) ); return $post_mime_types; } function admin_scripts_id1() { wp_register_script( 'pdf_metabox_js', get_stylesheet_directory_uri() . '/js/pdf_metabox.js' ); } function metabox_add_id1() { $post_types = array( 'myposttype','anotherposttype' ); $context = 'normal'; $priority = 'low'; foreach( $post_types as $post_type ) { add_meta_box( 'pdf_metabox_id1', __( 'PDF Box 1', 'pdf_metabox_id1' ), array( $this, 'pdf_metabox_id1' ), $post_type, $context, $priority ); wp_enqueue_media(); wp_enqueue_script( 'pdf_metabox_js' ); } } function pdf_metabox_id1( $post ) { $original_post = $post; echo $this->pdf_metabox_html_id1( $post->ID ); $post = $original_post; } function pdf_item_id1( $id ) { if(!$id) return; $pdf_url = esc_url_raw(wp_get_attachment_url($id)); $pdf = $pdf_url; return $pdf; } function pdf_metabox_html_id1( $post_id ) { $current_value = ''; $post_meta = get_post_custom($post_id); if( isset($post_meta['pdf_id_id1'][0] ) ) $current_value = $post_meta['pdf_id_id1'][0]; $return = ''; wp_nonce_field( plugin_basename( __FILE__ ), 'pdf_noncename' ); $return .= '<p>'; $return .= '<a title="'.__( 'PDF', 'pdf_metabox_id1' ).'" class="button button-primary insert-pdf-button" id="insert_pdf_button_id1" href="#" style="float:left">'.__( 'Upload / editiere PDF', 'pdf_metabox_id1' ).'</a><span id="pdf_spinner_id1" class="spinner" style="float:left"></span></p>'; $return .= '<div style="clear:both"></div>'; $return .= '<input type="hidden" name="pdf_id_id1" id="pdf_id_id1" value="'.$current_value.'">'; $return .= '<div style="clear:both"></div>'; $return .= '<div id="pdf_wrapper_id1">'; $pdf = $this->pdf_item_id1( $current_value ); if( empty( $pdf ) ) { $return .= '<p>Diesem Feld ist kein PDF hinterlegt.</p>'; } else { $return .= '<br />URL des PDF:<br /> '; $return .= $pdf; } $return .= '</div>'; return $return; } function pdf_save_postdata_id1($post_id){ if ( isset($_POST['post_type']) && 'post' == $_POST['post_type'] ) { if ( !current_user_can( 'edit_post', $post_id ) ) return; } if ( !isset( $_POST['pdf_noncename'] ) || ! wp_verify_nonce( $_POST['pdf_noncename'], plugin_basename( __FILE__ ) ) ) return; if(isset($_POST['pdf_id_id1']) ): update_post_meta($post_id, 'pdf_id_id1', sanitize_text_field( $_POST['pdf_id_id1'] ) ); else: if (isset($post_id)) { delete_post_meta($post_id, 'pdf_id_id1'); } endif; } function refresh_pdf_id1() { if(isset($_POST['id'])){ $item = $_POST['id']; if($item != '' && $item !=0){ $pdf = $this->pdf_item_id1( $item ); $ret = array(); if( !empty( $pdf ) ) {$ret['success'] = true; $ret['pdf'] = $pdf; } else { $ret['success'] = false; } } else { $ret['success'] = true; $ret['pdf'] = ''; } } else { $ret['success'] = false; } echo json_encode( $ret ); die(); } } $PDF_Metabox_id1 = new PDF_Metabox_id1(); in my theme i'm using: $post_meta_id1 = get_post_custom(get_the_ID()); $pdf_id_id1 = $post_meta_id1['pdf_id_id1'][0]; $pdf_url_id1 = wp_get_attachment_url($pdf_id_id1); ...and now i want to display the modified date of this field. any ideas?
As per the comment how do i save the modified date of this custom field and how do i get it? You may need to check every custom field in loop one by one if any of custom fields doesnot matched with the current post data then save a new custom field to post_meta. then retrieve in your template/page. Just an idea: <?php function attributes_save_postdata( $post_id ) { $postcustom = get_post_custom( $post_id ); $is_modified = false; foreach ( $postcustom as $key => $val ) { var_dump( $val ); // check your post custom values and $getpostmeta = get_post_meta( $post_id, 'your_meta_key', true ); // if database value ($getpostmeta) not equal to current post value($_POST['your_meta_key']) if ( $getpostmeta != $_POST['your_meta_key'] ) { $is_modified = true; // if found any custom field updated set $modified = true } } // if found any custom field updated add or update new date and time if ( $is_modified ) { $date = date( 'Y-m-d h:i:s' ); // example : 2016-02-02 12:00:00 current date and time update_post_meta( $post_id, '_modifieddate', $date ); } } add_action( 'save_post', 'attributes_save_postdata' ); Then in your theme. get modified date. $post_id = get_the_ID(); $getpostmeta = get_post_meta( $post_id, 'your_meta_key', true );
I think that wordpress built-in function about this would help you out even for the custom fields. <p>Last modified: <?php the_modified_time(); ?></p>