Note about WordPress Plugin Development

168
  1. Tải file và ảnh theo đg dẫn:

The plugins_url() function is one of the many functions that can be used in plugins to help find the location of files in a WordPress installation. Other useful functions include:

$icon_url = plugins_url( ‘favicon.ico’, __FILE__ );

  • get_theme_root(): Returns the address of the theme installation directory
  • get_template_directory_uri(): Retrieves the URI to the current theme’s files
  • admin_url(): Provides the address of the WordPress administrative pages
  • content_url(): Indicates where the wp-content directory can be found
  • site_url() and home_url(): Returns the site address
  • includes_url(): Provides the location of WordPress include files
  • wp_upload_dir(): Indicates the directory where user-uploaded files are stored

2. Filter hook:

add_filter( ‘hook_name’, ‘your_function_name’, [priority],
[accepted_args] );

Similar to action hooks, information about commonly used filter hooks can be found on the WordPress Codex (https://codex.wordpress.org/Plugin_API/Action_Reference) or on sites that provide raw function lists (for example, http://hookr.io).

It is also possible to learn about filter hooks by searching for occurrences of the apply_filters function in the WordPress code

3. Debug

Cấu hình file wp-config.php

define( ‘WP_DEBUG’, true );

Custom post type:

4. Option

Example: option as single value

5. Configuration page cho admin:

Thêm mục vào setting menu:

add_options_page( $page_title, $menu_title, $capability,
$menu_slug, $function );

Thêm vào một mục mới trong menu:

add_menu_page( $page_title, $menu_title, $capability,
$menu_slug, $function, $icon_url, $position );

add_submenu_page( $parent_slug, $page_title, $menu_title,
$capability, $menu_slug, $function );

Bỏ đi menu item:

remove_menu_page( ‘edit-comments.php’ );
remove_submenu_page( ‘options-general.php’, ‘options-permalink.php’ );

6. Security

wp_nonce_field( [$action], [$name], [$referer], [$echo] );

Redirect after post message:
wp_redirect( add_query_arg( array( ‘page’ => ‘ch2pho-my-google-analytics’, ‘message’ => ‘1’ ), admin_url( ‘options-general.php’ ) ) );

7. Setting API

| register_setting( $option_group, $option_name, $sanitize_callback );
within these parameters, the first option is a unique identifier for the settings group, the second is the name of the options array that will be used to store configuration data in the site database, while the third is the name of a callback function that will receive user input for validation.

Moving on to the second function used in this example, add_settings_section, the four parameters that it requires respectively indicate a unique identifier for the section, the title string that will be displayed when the section is rendered, a callback function that will be used to display a description for the section, and finally a page identifier that will be used to render all similar functions later within the plugin code:

The third function of the Settings API used in this recipe, add_settings_field, is called multiple times to define the fields that make up each section:

8. Custom post type

get_post_meta( $post_id, $field_name, $single ); // ‘single’|’array’