简体   繁体   中英

Localization jQuery confirm buttons in WordPress

I want to localize jQuery confirm buttons in WordPress. For this I coded in PHP file like this:

function Confirmation()
{
    // Register the script
    wp_register_script('eux-conf', '/js/eux-js.js');
    // Localize the script with new data
    $translation_array = array(
        'confirmation' => __('Confirmation', 'eux-loc'),
        'message' => __('Do you want to delete this item?', 'eux-loc'),
        'yes' => __('Yes', 'eux-loc'),
        'no' => __('No', 'eux-loc')
    );
    // Enqueued script with localized data.
    wp_enqueue_script('eux-conf');
    wp_localize_script('eux-conf', 'meta', $translation_array);
}
add_action('admin_print_scripts', 'Confirmation');

And jQuery codes:

jQuery('.delete').click(function()
{
    jQuery.confirm({
        'title': meta.confirmation,
        'message': meta.message,
        buttons: [
                    {
                        text: meta.yes,
                        classes: 'widget btn primary',
                        id: "yes",
                        onclick: function() {


                        }
                    },
                    {
                        text: meta.no,
                        classes: 'widget btn secondary',
                        id: "no",
                        onclick: 'close'
                    }
                ]

    });

EDIT: After David Lee's answer, I realized, if I write "This is a String" instead of meta.yes , I get 0 and 1 again like here:

        buttons: [
                    {
                        text: "This is a String",
                        classes: 'widget btn primary',
                        id: "yes",
                        onclick: function() {


                        }
                    },
                    {
                        text: meta.no,
                        classes: 'widget btn secondary',
                        id: "no",
                        onclick: 'close'
                    }
                ]

I think firstly, I must correct my buttons parameters, but I don't know.

But this gives me like here:

在此输入图像描述

You see, my buttos are Yes and No , but the code shows 0 and 1 . How do I correct this?

try

function Confirmation()
{
    // Register the script
    wp_register_script('eux-conf', '/js/eux-js.js');
    // Localize the script with new data
    $translation_array = array(
        'confirmation' => __('Confirmation', 'eux-loc'),
        'message' => __('Do you want to delete this item?', 'eux-loc'),
        'yes_s' => __('Yes', 'eux-loc'),
        'no_s' => __('No', 'eux-loc')
    );
    // Enqueued script with localized data.
    wp_localize_script('eux-conf', 'meta', $translation_array);//this one first
    wp_enqueue_script('eux-conf');

}
add_action('admin_enqueue_scripts', 'Confirmation');

and in jQuery:

jQuery('.delete').click(function()
{
    jQuery.confirm({
        'title': meta.confirmation,
        'message': meta.message,
        buttons: [
                    {
                        text: meta.yes_s,
                        classes: 'widget btn primary',
                        id: "yes",
                        onclick: function() {


                        }
                    },
                    {
                        text: meta.no_s,
                        classes: 'widget btn secondary',
                        id: "no",
                        onclick: 'close'
                    }
                ]

    });

i changed the order, you need to localize first then enqueue , also i changed the hook to admin_enqueue_scripts , and changed the name of the variables since no is a reserved one.

Try,

jQuery.confirm({
    'title': meta.confirmation,
    'message': meta.message,
    buttons: {
        yes: {
            text: meta.yes_s, // OR text: "'"+meta.yes_s+"'",
            classes: 'widget btn primary',
            id: "yes",
            onclick: function() {

            }
        },
        no: {
             text: meta.no_s, // OR text: "'"+meta.no_s+"'",
             classes: 'widget btn secondary',
             id: "no",
             onclick: 'close'
        },
    }
});

jQuery Confirm is just a plugin and not just one. There are one more plugins named jQuery.confirm. Which is your plugin jQuery-Confirm or BootboxJS or others. I suggest that update your plugin. Because Jaydip Nimavat's example works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM