Slide 1

Slide 1 text

wptexturize _wptexturize_pushpop_element wpautop shortcode_unautop seems_utf8 _wp_specialchars wp_specialchars_decode wp_check_invalid_utf8 utf8_uri_encode remove_accents sanitize_file_name sanitize_user sanitize_key sanitize_title sanitize_title_for_query sanitize_title_with_dashes sanitize_sql_orderby sanitize_html_class convert_chars balanceTags force_balance_tags format_to_edit zeroise backslashit trailingslashit untrailingslashit addslashes_gpc stripslashes_deep urlencode_deep rawurlencode_deep antispambot _make_url_clickable_cb _make_web_ftp_clickable_cb _make_email_clickable_cb make_clickable _split_str_by_whitespace wp_rel_nofollow wp_rel_nofollow_callback translate_smiley convert_smilies is_email wp_iso_descrambler _wp_iso_convert get_gmt_from_date get_date_from_gmt iso8601_timezone_to_offset iso8601_to_datetime popuplinks sanitize_email human_time_diff wp_trim_excerpt wp_trim_words ent2ncr wp_richedit_pre wp_htmledit_pre _deep_replace esc_sql esc_url esc_url_raw htmlentities2 esc_js esc_html esc_attr esc_textarea tag_escape wp_make_link_relative sanitize_option wp_parse_str wp_pre_kses_less_than wp_pre_kses_less_than_callback wp_sprintf wp_sprintf_l wp_html_excerpt links_add_base_url _links_add_base links_add_target _links_add_target normalize_whitespace wp_strip_all_tags sanitize_text_field wp_basename capital_P_dangit sanitize_mime_type sanitize_trackback_urls wp_slash wp_unslash get_url_in_content 10* useful WordPress functions *maybe more WordPress Meetup Milano - 5 May 2015

Slide 2

Slide 2 text

Giustino Borzacchiello @jubstuff

Slide 3

Slide 3 text

The #1 rule of WordPress development

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

DON’T TOUCH THE CORE

Slide 6

Slide 6 text

The core

Slide 7

Slide 7 text

The core too hot!

Slide 8

Slide 8 text

The core

Slide 9

Slide 9 text

The core

Slide 10

Slide 10 text

/wp-admin Functions and templates to bootstrap the WordPress admin. /wp-includes Most WordPress functionality is here.

Slide 11

Slide 11 text

DON’T TOUCH THE CORE ... but you can read it!

Slide 12

Slide 12 text

checked() Compares the first two arguments and if identical marks as checked.

Slide 13

Slide 13 text

wp-includes/general-template.php > checked()

Slide 14

Slide 14 text

wp-includes/general-template.php > checked()

Slide 15

Slide 15 text

selected() Compares the first two arguments and if identical marks as selected. wp-includes/general-template.php disabled() Compares the first two arguments and if identical marks as disabled.

Slide 16

Slide 16 text

Escaping functions. esc_*

Slide 17

Slide 17 text

?

Slide 18

Slide 18 text

Escaping functions. < becomes < esc_*()

Slide 19

Slide 19 text

➢ esc_sql ➢ esc_url ➢ esc_url_raw ➢ esc_js ➢ esc_html ➢ esc_attr ➢ esc_textarea esc_*() wp-includes/formatting.php

Slide 20

Slide 20 text

echo "Hello WordPress Meetup; alert('Ciao!');"; // Hello WordPress Meetup; esc_attr() wp-includes/formatting.php

Slide 21

Slide 21 text

echo esc_attr("Hello WordPress Meetup; alert('Ciao’) "); // Hello WordPress Meetup; // <script>alert('Ciao!');</script> esc_attr() wp-includes/formatting.php

Slide 22

Slide 22 text

// Retrieving data from user input... $name = ( isset( $_POST['name'] ) ) ? $_POST['name'] : ''; // ...and displaying it Example: esc_attr() wp-includes/formatting.php

Slide 23

Slide 23 text

Test if the current browser runs on a mobile device (smart phone, tablet, etc.). wp_is_mobile()

Slide 24

Slide 24 text

function add_my_cool_js_effect_on_desktop() { if ( wp_is_mobile() ) { return; } wp_enqueue_script('cool-js-effect', PATH_TO_MY_JS); } wp_is_mobile() wp-includes/vars.php

Slide 25

Slide 25 text

Utility functions for filters. __return_*()

Slide 26

Slide 26 text

➢ __return_empty_array ➢ __return_null ➢ __return_empty_string ➢ __return_zero ➢ __return_false ➢ __return_true __return_*() wp-includes/functions.php

Slide 27

Slide 27 text

add_filter( 'show_admin_bar', function(){ return false; } ); Example: __return_false() wp-includes/functions.php

Slide 28

Slide 28 text

add_filter( 'show_admin_bar', '__return_false' ); Example: __return_false() wp-includes/functions.php

Slide 29

Slide 29 text

Display a noindex meta tag. wp_no_robots()

Slide 30

Slide 30 text

/** * Display a noindex meta tag. * @since 3.3.0 */ function wp_no_robots() { echo "\n"; } wp_no_robots() wp-includes/general-template.php

Slide 31

Slide 31 text

// Add this to block search engines on page named 'no-search' add_action('init', function() { if(is_page('no-search')) { add_action( 'wp_head', 'wp_no_robots' ); } }); wp_no_robots() wp-includes/general-template.php

Slide 32

Slide 32 text

Merge user defined arguments into defaults array. wp_parse_args()

Slide 33

Slide 33 text

$defaults = ['count' => 5, 'orderby' => 'date', 'order' => 'asc',]; $params = ['orderby' => 'title', 'order' => 'desc',]; $options = wp_parse_args($params, $defaults); // $options = [ // 'count' => 5, // 'orderby' => 'title', // 'order' => 'desc', // ] wp_parse_args() wp-includes/functions.php

Slide 34

Slide 34 text

// MyWidget.php public function form( $instance ) { $defaults = array( 'num_entries' => 10, 'widget_title' => __( 'My title', 'ten-func' ), 'scale' => 10, ); $instance = wp_parse_args( (array) $instance, $defaults ); // display the form } wp_parse_args() wp-includes/functions.php

Slide 35

Slide 35 text

Extract a slice of an array, given a list of keys. wp_array_slice_assoc()

Slide 36

Slide 36 text

$arr = ['one'=> 1, 'two'=> 2, 'three'=> 3, 'four'=> 4, 'five'=> 5 ]; $odd = wp_array_slice_assoc($arr, ['one', 'three', 'five']); ------------------ array (size=3) 'one' => int 1 'three' => int 3 'five' => int 5 wp_array_slice_assoc() wp-includes/functions.php

Slide 37

Slide 37 text

Send a JSON response back to an Ajax request, indicating success. wp_send_json_success()

Slide 38

Slide 38 text

jQuery(document).ready(function($) { var data = { 'action': 'my_action', 'post_id': 4 }; jQuery.post(my_ajax_url, data, function(response) { alert('This is from PHP: ' + response); }); }); wp_send_json_success() wp-includes/functions.php

Slide 39

Slide 39 text

add_action('wp_ajax_nopriv_my_action', 'my_ajax_handler'); function my_ajax_handler() { $post_id = intval($_POST['post_id']); // Retrieve some $output_data related to that post wp_send_json_success($output_data); } wp_send_json_success() wp-includes/functions.php

Slide 40

Slide 40 text

jQuery(document).ready(function($) { var data = { 'action': 'my_action' }; jQuery.post(my_ajax_url, data, function(response) { alert('This is from PHP: ' + response); }); }); wp_send_json_success() wp-includes/functions.php { success: true, data: $output_data }

Slide 41

Slide 41 text

wp_send_json_error() Send a JSON response back to an Ajax request, indicating failure. wp-includes/functions.php wp_send_json() Send a JSON response back to an Ajax request.

Slide 42

Slide 42 text

Where do I start?

Slide 43

Slide 43 text

/wp-includes/general-template.php Mostly template tags. /wp-includes/functions.php Functions, functions everywhere.

Slide 44

Slide 44 text

/wp-includes/formatting.php Strings, dates, and general formatting functions. /wp-includes/pluggable.php Overwritable functions. You can be creative, here (if you know what you are doing).

Slide 45

Slide 45 text

Developer reference https://developer.wordpress.org/reference/

Slide 46

Slide 46 text

PHP xref http://phpxref.ftwr.co.uk/wordpress/

Slide 47

Slide 47 text

wpseek http://wpseek.com/

Slide 48

Slide 48 text

Try to find these functions: wp_list_pluck() wp_list_filter() wp_extract_urls() make_clickable() add_query_arg() _split_str_by_whitespace() wp_remote_get() wp_parse_id_list()

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

grazie.

Slide 51

Slide 51 text

grazie. Domande?

Slide 52

Slide 52 text

Earth image http://www.desy.de/information__services/press/press_releases/2013/pr_071113/index_eng.html