Search code examples
phpwordpresspluginswordpress-plugin-creation

How can I apply a filter when I check the checkbox in my Wordpress plugin?


I am building a wordpress plugin, and I want to apply and use a file that has the code bellow, when I check the checkbox inside the plugin's settings page.

Function I want to apply:

function greeklish_title_sanitizer($text) {

if ( !is_admin() ) return $text;
$expressions = array(

    '/[αάΑΆ]/u'   => 'a',
    '/[βΒ]/u'     => 'v',
    '/[γΓ]/u'     => 'g',
    '/[δΔ]/u'     => 'd',
    '/[εέΕΈ]/u'   => 'e',
    '/[ζΖ]/u'     => 'z',
    '/[ηήΗΉ]/u'   => 'i',
    '/[θΘ]/u'     => 'th',
    '/[ιίϊΙΊΪ]/u' => 'i',
    '/[κΚ]/u'     => 'k',
    '/[λΛ]/u'     => 'l',
    '/[μΜ]/u'     => 'm',
    '/[νΝ]/u'     => 'n',
    '/[ξΞ]/u'     => 'x',
    '/[οόΟΌ]/u'   => 'o',
    '/[πΠ]/u'     => 'p',
    '/[ρΡ]/u'     => 'r',
    '/[σςΣ]/u'    => 's',
    '/[τΤ]/u'     => 't',
    '/[υύϋΥΎΫ]/u' => 'y',
    '/[φΦ]/iu'    => 'f',
    '/[χΧ]/u'     => 'ch',
    '/[ψΨ]/u'     => 'ps',
    '/[ωώ]/iu'    => 'o',
    
    '/[αΑ][ιίΙΊ]/u'                             => 'e',
    '/[οΟΕε][ιίΙΊ]/u'                           => 'i',
    '/[αΑ][υύΥΎ]([θΘκΚξΞπΠσςΣτTφΡχΧψΨ]|\s|$)/u' => 'af$1',
    '/[αΑ][υύΥΎ]/u'                             => 'av',
    '/[εΕ][υύΥΎ]([θΘκΚξΞπΠσςΣτTφΡχΧψΨ]|\s|$)/u' => 'ef$1',
    '/[εΕ][υύΥΎ]/u'                             => 'ev',
    '/[οΟ][υύΥΎ]/u'                             => 'ou',
    '/(^|\s)[μΜ][πΠ]/u'                         => '$1b',
    '/[μΜ][πΠ](\s|$)/u'                         => 'b$1',
    '/[μΜ][πΠ]/u'                               => 'b',
    '/[νΝ][τΤ]/u'                               => 'nt',
    '/[τΤ][σΣ]/u'                               => 'ts',
    '/[τΤ][ζΖ]/u'                               => 'tz',
    '/[γΓ][γΓ]/u'                               => 'ng',
    '/[γΓ][κΚ]/u'                               => 'gk',
    '/[ηΗ][υΥ]([θΘκΚξΞπΠσςΣτTφΡχΧψΨ]|\s|$)/u'   => 'if$1',
    '/[ηΗ][υΥ]/u'                               => 'iu',

);



$text = preg_replace( array_keys($expressions), array_values($expressions), $text );
return $text;

}

add_filter('sanitize_title', 'greeklish_title_sanitizer', 1);

My plugin's main file:

<?php
/*
Plugin Name: random permalink converter
Description: This is my custom Plugin!
Author: Jordan Piperkata
Version: 1.0.0
Tested up to: 5.9
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
defined( 'ABSPATH' ) or die;

function gpc_add_settings_page() {
    add_options_page( 'GRPC', 'randomPermalinksConverter', 'manage_options', 'random-permalink-converter', 'gpc_render_plugin_settings_page' );

    register_setting(
        'gpc_settings', // settings group name
        'homepage_text', // option name
        'sanitize_text_field' // sanitization function
    );

    add_settings_section(
        'some_settings_section_id', // section ID
        '', // title (if needed)
        '', // callback function (if needed)
        'random-permalink-converter' // page slug
    );

    add_settings_field(
        'homepage_text',
        'Homepage text',
        'text_field_html', // function which prints the field
        'random-permalink-converter', // page slug
        'some_settings_section_id', // section ID
        array( 
            'label_for' => 'homepage_text',
            'class' => 'gpc-class', // for <tr> element
        )
    );
}

add_action( 'admin_menu', 'gpc_add_settings_page' );

function gpc_render_plugin_settings_page() {

    echo '<div class="wrap">
    <h1>random Permalink Converter Settings Page</h1>
    <form method="post" action="options.php">';
            
        settings_fields( 'gpc_settings' ); // settings group name
        do_settings_sections( 'random-permalink-converter' ); // just a page slug
        submit_button();

    echo '</form></div>';

}

function text_field_html(){

    $text = get_option( 'homepage_text' );

    if ( $text == 'on' ) {
        printf(
            '<input type="checkbox" id="homepage_text" name="homepage_text" checked="checked" />',
            esc_attr( $text )
        );
        echo 'Toggle is: '.$text;
    } else {
        printf(
            '<input type="checkbox" id="homepage_text" name="homepage_text" />',
            esc_attr( $text )
        );
        echo 'Toggle is: Off';
    }
        
}

function permalink_set(){

    add_filter('sanitize_title', 'random_title_sanitizer', 1);

}

Recap:

1): I use this basic piece of code in my main file, to create a custom plugin settings page. 2): I have a function named text_field_html that creates a checkbox, and I Want when the checkbox is checked to apply the filter of the function's file. But including the file after the if that checks if the checkbox is checked doesn't work, because this plugin is supposed to echo a string. While if I simply use the function's code in the main file directly, it works fine but of course without the checkbox being checked first.

Questions:

What am I doing wrong here? Or perhaps how can I call this type of add_filter functions of wordpress and keep them active when I check the checkbox in the setting's page and save?

Thanks in advance for your time!


Solution

  • I hope I understand what you mean. Yo should check the option homepage_text and to add the filter.

    add_action('init', 'permalink_set');
    function permalink_set(){
        if  (get_option( 'homepage_text' ) == 'on'){
            add_filter('sanitize_title', 'random_title_sanitizer', 1);
        }
    }