- start a basic plugin that can send code to front

- for the moment it send js scripts, css, and variables
This commit is contained in:
asus
2024-02-07 18:46:28 +01:00
parent e3ae101fdd
commit b943682047
7 changed files with 98 additions and 3 deletions

View File

@@ -8,14 +8,50 @@ Version: 1.1.0
Author URI:
*/
include_once(dirname(__FILE__) . '/utils/consolelog.php');
/* * * * * * * * * * * * * * * * * * * * * * * *
* inclusions
*/
include_once(dirname(__FILE__) . '/utils/console_log.php');
include_once(dirname(__FILE__) . '/utils/add_to_front.php');
/* * * * * * * * * * * * * * * * * * * * * * * *
* plugin shortcode
*/
function main_shortcode() {
consolelog("hello from php");
# send styles files by name, without extension .css
add_css_to_front( array(
"mystyle",
));
# send scripts files by name, without extension .js
add_script_to_front( array(
"myscript",
"myscript2",
"myscript3",
));
$myvar_1 = "I am one";
$myvar_2 = "I am two";
# maybe a future version of php will allow to
# get the name of a variable :
# https://wiki.php.net/rfc/nameof
# but for now i have to declare both the variable and its name
# (more : https://stackoverflow.com/q/255312/9497573)
add_var_to_front( array(
"myvar_1" => $myvar_1,
"myvar_2" => $myvar_2,
));
}
add_shortcode('fipf_wp_plugin', 'main_shortcode');
?>

View File

@@ -0,0 +1,5 @@
const title = document.querySelector(".has-text-align-center.wp-block-post-title");
title.innerHTML = "coucou ;)";
console.log("myvar_1: " + myvar_1);

View File

@@ -0,0 +1,4 @@
const title2 = document.querySelector(".wp-block-quote");
title2.innerHTML = "ho boy !";

View File

@@ -0,0 +1,5 @@
const title3 = document.querySelector(".has-text-align-center.wp-block-post-title");
title3.innerHTML = "bye bye";
console.log("myvar_2: " + myvar_2);

View File

@@ -0,0 +1,3 @@
.has-text-align-center.wp-block-post-title {
border: 1px solid green;
}

View File

@@ -0,0 +1,42 @@
<?php
function add_css_to_front($style_files_arr) {
//wp_enqueue_style( <give_it_a_name>, /url/to/file, [depends on], version, media );
$previous_file = '';
foreach ($style_files_arr as $file) {
$file_url = plugin_dir_url(__DIR__).'styles/'.$file.'.css';
$file_path = plugin_dir_path(__DIR__).'styles/'.$file.'.css';
$file_version = date("ymd-Gis", filemtime($file_path));
wp_enqueue_style( $file, $file_url, $previous_file, $file_version, '');
$previous_file = $file;
}
}
function add_script_to_front($script_files_arr) {
//wp_enqueue_script(<give_it_a_name>, /url/to/file, [depends on], version, defer? );
$previous_file = '';
foreach ($script_files_arr as $file) {
$file_url = plugin_dir_url(__DIR__).'scripts/'.$file.'.js';
$file_path = plugin_dir_path(__DIR__).'scripts/'.$file.'.js';
$file_version = date("ymd-Gis", filemtime($file_path));
console_log("file_url: " . $file_url);
wp_enqueue_script( $file, $file_url, $previous_file, $file_version, true);
$previous_file = $file;
}
}
function add_var_to_front($var_array) {
foreach ($var_array as $key => $var)
{
$js_var = 'const ' . $key . ' = ';
$js_var .= json_encode($var);
$js_var .= ';';
console_log("in php, js_var: " . $js_var);
wp_add_inline_script('myscript', $js_var, 'before');
}
}
?>

View File

@@ -1,7 +1,7 @@
<?php
// https://stackify.com/how-to-log-to-console-in-php/
function consolelog($output) {
function console_log($output) {
$js_code = 'console.log(' . json_encode($output, JSON_HEX_TAG) . ');';
$js_code = '<script>' . $js_code . '</script>';
echo $js_code;