WordPress Notes

25-02-2024

Adding a tag to the Head Tag

<?php 
// Add this into functions.php

function hook_add_petite_vue_to_wp_head(): void {
    echo '<script src="https://unpkg.com/petite-vue" defer init></script>';
}

add_action('wp_head', 'hook_add_petite_vue_to_wp_head');
?>

Adding a tag to the Footer Tag

<?php 
// Add this into functions.php

function hook_add_petite_vue_to_wp_footer(): void {
    echo '<script src="https://unpkg.com/petite-vue" defer init></script>';
}

add_action('wp_footer', 'hook_add_petite_vue_to_wp_head');
?>

Creating a plugin

mkdir plugin-name
cd plugin-name
touch puglin-name.php

Inside plugin-name.php

<?php
/**
 * Plugin Name: My Custom Blocks
 * Description: Una collezione di blocchi personalizzati per Gutenberg.
 * Version: 1.0
 * Author: Your Name
 */

Creating a plugin with multiple plugins

The result is

  • plugins/plugin-name/src/block-1
  • plugins/plugin-name/src/block-2

Inside the plugins folder, create a static block. This procedure will also create the plugin. Just follow the CLI. Name it with the plugin name, for example "plugin-name"

npx @wordpress/create-block@latest

In your plugin-name/ folder, remove some generated files

cd src/
sudo rm -rf block.json edit.js editor.scss style.scss index.js save.js view.js

Inside the src/ folder, create your first block, either static or dynamic

npx @wordpress/create-block@latest --no-plugin

Remember to use Update in plugin-name.php

register_block_type( __DIR__ . '/build/<your block name>' );

Run a npm run start and you are ready to go

Register an endpoint inside the plugin

Inside the plugin-name.php file

<?php
/**
 * Plugin Name: My Custom Plugin
 * Description: Questo è un esempio di plugin che aggiunge un endpoint dell'API REST di WordPress.
 * Version: 1.0
 * Author: Il tuo nome
 */

add_action('rest_api_init', function () {
    register_rest_route('myplugin/v1', '/helloworld/', array(
        'methods' => 'GET',
        'callback' => 'my_plugin_helloworld_callback',
    ));
});

function my_plugin_helloworld_callback($request) {
    $response = array(
        'message' => 'Hello World',
    );

    return new WP_REST_Response($response, 200);
}

Register a callback function that executes a query

function my_plugin_get_recent_posts_callback($request) {
    global $wpdb;

    $query = "
        SELECT post_title, post_date
        FROM {$wpdb->posts}
        WHERE post_status = 'publish' 
        AND post_type = 'post'
        ORDER BY post_date DESC
        LIMIT 5
    ";

    $recent_posts = $wpdb->get_results($query);

    if (empty($recent_posts)) {
        return new WP_Error('no_posts', 'Nessun post trovato', array('status' => 404));
    }

    return new WP_REST_Response($recent_posts, 200);
}

Register a callback function that sends an email

function my_plugin_send_email_callback( $request ) {
    $params = $request->get_json_params();
    $email = isset( $params['email'] ) ? sanitize_email( $params['email'] ) : null;
    $message = isset( $params['message'] ) ? sanitize_textarea_field( $params['message'] ) : '';

    if ( ! is_email( $email ) ) {
        return new WP_Error( 'invalid_email', 'Indirizzo email non valido.', array( 'status' => 400 ) );
    }

    $subject = 'Messaggio dal tuo sito WordPress';
    $headers = array('Content-Type: text/html; charset=UTF-8');
    
    $sent = wp_mail( $email, $subject, $message, $headers );

    if ( $sent ) {
        return new WP_REST_Response( 'Mail inviata con successo.', 200 );
    } else {
        return new WP_Error( 'mail_send_fail', 'Errore nell\'invio della mail.', array( 'status' => 500 ) );
    }
}

add_action( 'rest_api_init', function () {
    register_rest_route( 'myplugin/v1', '/send-email/', array(
        'methods' => WP_REST_Server::CREATABLE, // Equivalent to 'POST'
        'callback' => 'my_plugin_send_email_callback',
        'permission_callback' => function () {
            // Verifica che l'utente abbia il permesso di inviare la mail (modifica qui secondo i tuoi requisiti)
            return current_user_can( 'edit_posts' );
        }
    ) );
} );