Created
June 26, 2012 09:40
-
-
Save reinislejnieks/2994693 to your computer and use it in GitHub Desktop.
wordpress - general
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// get all options in array from OPTIONS table | |
get_alloptions(); | |
// cache database query, so you don't have to query database again | |
$activity_object = wp_cache_get($id,'activity'); | |
if(false === $activity_object){ | |
$activity_object = $wpdb->get_row( | |
$wpdb->prepare("SELECT*FROM | |
$wpdb->activity WHERE ID = %d",$id)); | |
wp_cache_set($id,$activity_object,'activity'); | |
} | |
// Set transient | |
$value = get_transient('big_data'); | |
if(false === $value){ | |
// do something that tkaes fair amount of time | |
$response = wp_remote_get($url); | |
$value = wp_remote_retrieve_body($response); | |
set_transient('big_data',$value, 60*60*24); | |
} | |
echo $value; | |
/*==============================*/ | |
/* use AJAX in wp the right way */ | |
/*==============================*/ | |
// submiting data through link | |
// first create a nonce for security | |
$nonce = wp_create_nonce("my_value_nonce"); | |
// create link to admin-ajax.php | |
$link = admin_url('admin-ajax.php?action=my_action&value_one='.$some_value_one.'&nonce='.$nonce); | |
// output the javascript fallback link | |
echo '<a href="'.$link.'" data-nonce="'.$nonce.'" data-some-value="value"></a>'; | |
// adding the actions to wp - wp creates action hook wp_ajax_ + the action name submitted via link action attribute | |
// first hook is for logged-in users, the second for all other users | |
add_action("wp_ajax_my_action","my_action"); | |
add_action("wp_ajax_nopriv_my_action","my_action_must_login"); | |
// create the function | |
function my_action(){ | |
// first check the nonce, if it fails, then exit | |
if(!wp_verify_nonce($_POST['nonce'],"my_value_nonce")){ | |
exit("No naughty business please!"); | |
} | |
// here goes some logic, passed values can be accessed through | |
// $_POST['...'] in this case - $_POST['value_one'] | |
// create values to send back through AJAX | |
// in this case an array | |
$result['output_one'] = "some output value"; | |
$result['output_two'] = "another output value"; | |
// Check whether the action was initiated through an AJAX call. | |
// If so, then we use the json_encode() function to prepare the | |
// array for our JavaScript code. If the call was made without AJAX, | |
// then we simply send the user back to where they came from. | |
// We could also put array in a session variable or cookie. | |
if(!empty($_SERVER['HTTP_X_REQUESTED_WIDTH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WIDTH']) == 'xmlhttprequest'){ | |
$result = json_encode($result); | |
echo $result; | |
}else{ | |
header("Location: ".$_SERVER["HTTP_REFERER"]); | |
} | |
// it is madatory to end script with die() otherwise output would be -1 along with the result | |
die(); | |
} | |
// create message for logged-out users | |
function my_action_must_login(){ | |
echo "You must login to use this functionality!"; | |
die(); | |
} | |
// Add AJAX | |
// enqueue jQuery and custom AJAX script | |
function my_scripts(){ | |
// register and localize my_script | |
wp_register_script( | |
"my_script", | |
get_template_directory_uri().'/functions/scripts/my_script.js', | |
array('jquery') | |
); | |
// Using localize script in this case ensures that in our my_script.js file, | |
// we will be able to use myAjax.ajaxurl, which contains the URL of our admin-ajax.php file. | |
wp_localize_script('my_script', 'myAjax', array('ajaxurl'=>admin_url('admin-ajax.php'))); | |
wp_enqueue_script('jquery'); | |
wp_enqueue_script('my_script'); | |
} | |
add_action('wp_enqueue_scripts', 'my_scripts'); | |
// and some example javascript with AJAX call | |
?> | |
<script> | |
jQuery(document).ready(function(){ | |
jQuery(".someclass").click(function(e){ | |
/* prevents default click */ | |
e.preventDefault(); | |
nonce = jQuery(this).attr("data-nonce"); | |
value = jQuery(this).attr("data-some-value"); | |
jQuery.ajax({ | |
type : "post", | |
dataType : "json", | |
url : myAjax.ajaxurl, | |
data :{action:"my_action", some_value_one : value, nonce : nonce}, | |
success: function(response){ | |
if(response.type == "success"){ | |
/* do something here*/ | |
}else{ | |
alert("Error!"); | |
} | |
} | |
}); | |
}); | |
}); | |
</script> | |
<?php | |
/* HTACCESS */ | |
/* | |
Allow access to the wp-login file to only certain IP addresses (ie yours!) | |
<Files wp-login.php> Order deny,allow Deny from All Allow from 123.456.789.0 </Files> | |
Rename the .htaccess file (thus making it harder to find) | |
# rename htaccess files AccessFileName ht.access | |
Protect the .htaccess | |
# STRONG HTACCESS PROTECTION</code> <Files ~ "^.*\.([Hh][Tt][Aa])"> order allow,deny deny from all satisfy all </Files> | |
Disable directory browsing (thus stopping visitors finding what plugins you’re using etc) | |
# disable directory browsing Options All -Indexes | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment