I m using WP pointers in my code...Pointers not display again after i dismiss it once... i deleted my plugin and again activate it but wp pointers not display again... If i install a new wordpress then WP Pointers display but when i dismiss it, then it never comes agains.. Is there a way that when plugin is activated wp pointers appears again...?? Here is my code
function thsp_enqueue_pointer_script_style( $hook_suffix ) {
// Assume pointer shouldn't be shown
$enqueue_pointer_script_style = false;
// Get array list of dismissed pointers for current user and convert it to array
$dismissed_pointers = explode( ',', get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
// Check if our pointer is not among dismissed ones
if( !in_array( 'thsp_pointer', $dismissed_pointers ) ) {
$enqueue_pointer_script_style = true;
// Add footer scripts using callback function
add_action( 'admin_print_footer_scripts', 'thsp_pointer_print_scripts' );
}
// Enqueue pointer CSS and JS files, if needed
if( $enqueue_pointer_script_style ) {
wp_enqueue_style( 'wp-pointer' );
wp_enqueue_script( 'wp-pointer' );
}}add_action( 'admin_enqueue_scripts', 'thsp_enqueue_pointer_script_style' );
function thsp_pointer_print_scripts() {
$pointer_content = "<h3>My New Plugin</h3>";
$pointer_content .= "<p>If you ever activated a plugin, then had no idea where its settings page is, raise your hand.</p>";
?>
<script type="text/javascript">
//<![CDATA[
jQuery(document).ready( function($) {
$('#toplevel_page_settings').pointer({
content:'<?php echo $pointer_content; ?>',
position:{
edge: 'left', // arrow direction
align: 'center' // vertical alignment
},
pointerWidth: 350,
close:function() {
$.post( ajaxurl, {
pointer: 'thsp_pointer', // pointer ID
action: 'dismiss-wp-pointer'
});
}
}).pointer('open');
});
//]]>
</script>
Dismissed pointers are stored in the wp_usermeta table, for each user, sub meta key dismissed_wp_pointers. Ideally, your plugin should remove the IDs of your pointers for each user on deactivation (see register_deactivation_hook), if you want them to reappear on reactivation.
Related
I have a custom post type called request created with Toolset plugin.
I need to add a button in these admin pages (only for this custom post type):
new request (post-new.php)
edit request (post.php)
When admin submit the button I need to send an email (no problem for this) with info about the post.
How can I add the button and a callback to submit?
One way to add new elements to the Post Edit screen is via metaboxes.
Please check the following code:
/**
* Adds a call-to-action metabox to the right side of the screen under the "Publish" box.
*/
function wp645397_add_cta_metabox() {
add_meta_box(
'wp645397_request_cta',
'Call-to-action title here', /* This is the title of the metabox */
'wp645397_request_cta_html',
'request', /* the request post type */
'side',
'high'
);
}
add_action( 'add_meta_boxes', 'wp645397_add_cta_metabox' );
/**
* Output the HTML for the call-to-action metabox.
*/
function wp645397_request_cta_html() {
global $post;
// Nonce field to validate form request came from current site
wp_nonce_field( 'request_send_post_details', 'request_cta_nonce' );
// Output the call-to-action button
?>
Call-to-action button text here
<script>
/**
* We'll handle the button via Ajax to avoid having to reload the screen.
*/
jQuery(function($){
// Handle button click
$( "#wp645397_request_cta #btn-call-to-action" ).on("click", function(e){
e.preventDefault();
// Get the security nonce
var nonce = $(this).parent().find("#request_cta_nonce").val();
// Send the data via AJAX
$.post(
ajaxurl,
{
action: 'send_request_email',
nonce: nonce,
postid: <?php echo $post->ID; ?>
},
function( response ){
// Do something after the data has been sent
if ( 'OK' == response ) {
alert( 'Info sent' );
}
else {
alert( 'Something went wrong, try again' );
}
}
);
});
});
</script>
<?php
}
/**
* Handles CTA AJAX.
*/
function wp645397_send_request_email(){
// Invalid nonce
if ( !isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'request_send_post_details' ) ) {
wp_die( 'error' );
}
$post_ID = $_POST['postid'];
// Get post data
$post = get_post( $post_ID );
// Get post title
$post_title = $post->post_title;
// Get custom field, etc
// #TODO
// Send e-mail with data
// #TODO
wp_die('OK');
}
add_action( 'wp_ajax_send_request_email', 'wp645397_send_request_email' );
What it does:
Adds a custom metabox right above the Publish box on the right.
When the user clicks on the "Call-to-action button text here" button (remember to rename it!), it'll send the ID of the current post (request) via Ajax to a function called wp645397_send_request_email, where you need to process the data and send the e-mail.
I added some comments in the code that should explain what's going on. If you have any questions don't hesitate to ask, ok?
I am not sure what options toolset plugin provides but if I were to do this programatically, I would do something like
$post_types = array('post', 'page');
$args = array(
'public' => true,
'_builtin' => false,
);
$output = 'names'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'
$fetch_post_types = get_post_types($args, $output, $operator);
foreach ($fetch_post_types as $key => $value) {
if ($value != 'vocabulary' && $value != 'augmentation') {
array_push($post_types, $value);
}
}
add_meta_box('email_sender_post_type_metabox', __('Email Options', 'seoaipdl'), 'email_sender_post_type_metabox', $post_types, 'side', 'high', null);
function email_sender_post_type_metabox() {
//Button HTML code here
}
add_action('save_post', 'save_meta_data_function', 10, 2);
function save_meta_data_function($id, $data) {
if (!current_user_can("edit_post", $id)) {
return $data;
}
if (defined("DOING_AUTOSAVE") && DOING_AUTOSAVE) {
return $data;
}
\\$_POST variable will have your submit button here.
}
Alternatively You can add a button using jquery and trigger your email function using ajax on this button click.
To do so you will need to look into admin_enqueue_script and wp_localize_script
I am using this script in an attempt to set the visibility styling of a div and store it as a cookie, site-wide. My understanding is that path=/ should set the cookie to store at the root and thus be available to all pages - passing the value to the other pages to either keep the div's visibility, visible or hidden, depending on the users preference. However, checking the output, it appears as though the cookie is only being stored 'per-page'. What am I missing? I have placed the script in the header and it is loaded on every custom post/page of my wp theme. (omitted) - there is a toggle button top left of the page, show/hide comments.
// Placed above </head> tag on my header-webmockups.php file
<script type="text/javascript">
$( function () {
var toggle = $( '.toggle' );
var comments = toggle.find( '.comments' );
if ( $.cookie( 'divState' ) == 'visible' )
comments.show();
else
comments.hide();
toggle.find( 'a' ).click( function () {
if ( comments.is( ':visible' ) )
$.cookie( 'divState', 'hidden' );
else
$.cookie( 'divState', 'visible' );
comments.toggle();
} );
} );
$.cookie( "divState", 1, {
expires: 10000
} );
</script>
// Placed at the top of my comments.php loop (toggle div closes after page content)
<div class="toggle"><a>Show/Hide Comments</a>
// Placed after <?php wp_head(); ?> of post's custom header.php
<?php wp_enqueue_script("jquery-cookie", get_stylesheet_directory_uri().'/js/lib/jquery.cookie.js', array( 'jquery' ), '0'); ?>
You need to add expiration date to your cookie.
For example
$.cookie("test", 1, { expires : 10000 });
As i see from your URL and code, you haven't set it and it works in current session only.
So after all your script block should look like so: (remove current block entirely and paste this one)
<script type="text/javascript">
jQuery( function ($) {
var toggle = $( '.toggle' );
var comments = toggle.find( '.comments' );
if ( $.cookie( 'divState' ) == 'visible' )
comments.show();
else
comments.hide();
toggle.find( 'a' ).click( function () {
if ( comments.is( ':visible' ) )
$.cookie( 'divState', 'hidden', {expires: 10000, path: '/'} );
else
$.cookie( 'divState', 'visible', {expires: 10000, path: '/'} );
comments.toggle();
} );
} );
</script>
I want to upload multiple images using wordpress.
Here is my code which is using to single upload
if ( ! function_exists( 'wp_handle_upload' ) )
require_once( ABSPATH . 'wp-admin/includes/file.php' );
$uploadedfile = $_FILES['photo'];
$upload_overrides = array( 'test_form' => FALSE );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile ) {
echo "File is valid, and was successfully uploaded.\n";
var_dump( $movefile);
}
else {
echo "Possible file upload attack!\n";
}
How I can upload multiple images using wordpress?
Try to implement like this:
$files = $_FILES['photo'];
foreach ($files['photo'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
wp_handle_upload($file);
}
}
You might be able to achieve what you're trying to do easier by using the native WordPress Media Uploader.
I had a good resource bookmarked at one point but can't seem to find it. I ran into a situation a few months ago that required the same thing. This is what I ended up doing:
Loading the WordPress image scripts into the beginning of my PHP script:
require_once( ABSPATH . 'wp-admin/includes/image.php' );
Then you can call the uploader on element click using jQuery like this:
jQuery(document).ready(function($) {
// Define a variable to be used to store the frame data
var file_frame;
var wp_media_post_id = wp.media.model.settings.post.id; // Store the old id
var set_to_post_id = 999; // Set this
$('#upload_button').live('click', function( event ){
element = $(this);
event.preventDefault();
// If the media frame already exists, reopen it.
if (file_frame) {
// Set the post ID to what we want
file_frame.uploader.uploader.param('post_id', set_to_post_id);
// Open frame
file_frame.open();
return;
} else {
// Set the wp.media post id so the uploader grabs the ID we want when initialised
wp.media.model.settings.post.id = set_to_post_id;
}
// Create the media frame.
file_frame = wp.media.frames.file_frame = wp.media({
title: $(this).data('uploader_title'),
button: {
text: $(this).data('uploader_button_text'),
},
multiple: true // Set to false to allow only one file to be selected
});
// When an image(s) have been selected, run a callback.
file_frame.on('select', function() {
// Do something if necessary
function_to_fire();
});
// Finally, open the modal
file_frame.open();
});
});
There is an attachment object passed to the callback function which will contain all of the IDs and such for the attachments uploaded.
The set_to_post_id is the id of the post/page you want to "attach" the media to. If you're not trying to attach them to a post, you shouldn't need to worry about this and some of the above code will not apply.
I need a simple click counter for links in my wordpress posts. I used google a lot but couldnt find a working one. I want to count clicks from a specific class and store it in post_meta or as a custom field to display the count later.
here is some code i found but i cant get it to work..nothting happens when i click the link.
js
jQuery(document).ready(function($) {
$("a[link-out]").click(function() {
var linkout = $(this).attr("link-out");
var data = {
action: 'my_action',
postid: linkout
};
$.post(MyAjax.ajaxurl, data);
});
});
php
wp_enqueue_script( 'my-ajax-request', plugin_dir_url( __FILE__ ) . 'js/countclicks.js', array( 'jquery' ) );
// declare the URL to the file that handles the AJAX request (wp-admin/admin-ajax.php)
wp_localize_script( 'my-ajax-request', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
add_action('wp_ajax_my_action', 'my_action_callback');
add_action('wp_ajax_nopriv_my_action', 'my_action_callback');
function my_action_callback() {
global $wpdb;
$post_id = $_POST['postid'];
$post_id = mysql_real_escape_string($post_id);
$wpdb->query("UPDATE wp_postmeta SET meta_value = meta_value+1 WHERE post_id = '$post_id' AND meta_key = 'clicks_out'");
}
here is another one i found here on stackoverflow, but also doenst work
coding ajax click counter on wordpress
I have a set of different map types (styles). As you can see in the simplified example, I try to switch the map types with an onClick event.
This works great for the default - by google.maps provided - styles. But when I try to switch to my custom style, I have a delay of ~2-3 seconds (local environment) and then get a completely grey map (nothing on the tiles).
I know that the map style itself works (not the same as in the linked example), because it's my initial map style. So only the switch back to my custom style is not working. Sadly I get nothing in the console and don't know how I could debug this.
<?php
// Inside the init(); function:
?>
var custom_style = [
{
featureType: 'water'
,stylers: [
{ hue: "#009ee0" }
,{ saturation: 100 }
,{ lightness: 0 }
]
}
];
var CUSTOMMAPTYPE = new google.maps.StyledMapType(
custom_style,
// Options:
{
alt: "Show Custom style",
name: "custom"
}
);
<?php
// Inside the view
$map_types = array(
'Roadmap'
,'Terrain'
,'Satellite'
,'Hybrid'
,'CustomMapType'
);
foreach ( $map_types as $index => $type )
{
?>
<span
class="map-type"
onClick="
my_map.setMapTypeId( google.maps.MapTypeId.<?php echo strtoupper( $type ); ?> );
// This is funny: I can access all mapType objects without a problem:
console.log( my_map.mapTypes.<?php echo strtolower( $type ); ?> );
"
id="<?php echo strtolower( $type ); ?>"
>
<?php echo $type; ?>
</span>
<?php
}
?>
Any help is highly appreciated.
Thanks in advance
EDIT:
Here's something other I tried (from inside my init(); fn, that does the setup for the map):
// The map type control DOM/UI Element
var map_type_controls = document.getElementsByClassName( 'map-type' );
// using jQuery to add a DOM listener function for click events to the UI elements:
jQuery.each( map_type_controls, function() {
google.maps.event.addDomListener( this, 'click', function() {
var control_id = jQuery( this ).attr( 'id' );
var control = control_id.toUpperCase();
// uncomment to debug:
// Shows all map type control DOM element correct
// console.log( control );
// Shows all registered mapType objects incl. the custom one correct:
// console.log( my_map.mapTypes );
my_map.setMapTypeId( google.maps.MapTypeId[ control ] );
// Per recommendation - doesn't help
google.maps.event.trigger( atlas_map, "resize" );
} );
} );
Normally when you modified a property of a map, let's say a new coordinates or a zoom, even if you change the size of the container (div), you should trigger the event called resize it's the only way to avoid this problem, now, i don't know if this works when you change the type of the map but you should try, the code it's the following:
google.maps.event.trigger(map, "resize");