0

This is the whole code actually →

<script>
            jQuery.noConflict();
            jQuery(document).ready(function()
                {

                    jQuery('#mailchimp_submit-<?php echo $random?>').click(function()
                    {
                        jQuery('#process-<?php echo $random?>').css('display','block');
                        var datastring = '&name=' + escape(jQuery('#name-<?php echo $random?>').val()) + '&email=' + escape(jQuery('#email-<?php echo $random?>').val()) + '&api_key=<?php echo $mailchimp_api_key;?>&list_id=<?php echo $mailchimp_list_id;?>';
                        jQuery.ajax({
                            url: '<?php echo rest_url('/chimp/rest/process_chimp/');?>',
                            data: datastring,
                            success: function(msg)
                            {
                                jQuery('#process-<?php echo $random?>').css('display','none');
                                jQuery('#newsletter_msg-<?php echo $random?>').html(msg);
                            },
                            error: function(msg)
                            {
                                jQuery('#process-<?php echo $random?>').css('display','none');
                                jQuery('#newsletter_msg-<?php echo $random?>').html(msg);
                            }
                        });
                        return false;
                    });
            });
        </script>

There are a lot of confusions:

1 →

How to handle this as this has a semicolon →

jQuery.noConflict();

should I do it like this:

$output = 'jQuery.noConflict();'; or

$output = 'jQuery.noConflict()'; or

$output = 'jQuery.noConflict();'

2 →

How to handle one that has a function →

jQuery(document).ready(function()

Like this →

$output =  'jQuery(document).ready(function()';

How to handle this →

4 →

jQuery.ajax({
              url: '<?php echo rest_url('/chimp/rest/process_chimp/');?>',
              data: datastring,
              success: function(msg)
              {

5 →

return false;

The above also needs to be modified?

Please guide me where I am going wrong, and where my approach and understanding is not correct. This is an Extension of this old question.

All the above code is a part of the widget that I am trying to convert into a shortcode.

  • Can some one help me in this o if there is some better way? – The WP Novice Sep 12 '17 at 20:55
  • 1
    I've added an answer, but again, this has nothing to do with WordPress. Most of your questions lately have been about basic PHP and JavaScript syntax. I strongly suggest you get the fundamentals of PHP and JavaScript down before you get too complex in WordPress, and if you need help with that Stack Overflow is the right place to ask. – Jacob Peattie Sep 13 '17 at 3:16
1

For large amounts of HTML you're better off using output buffering with ob_start() and ob_get_clean(). The documentation for it is here, but the short version is that it lets you capture output to return later. That way you can just output HTML like you would in a template, but capture it and return it at the end:

function wpse_279827_shortcode() {
    // Do PHP stuff.
    $message = 'Hello world!';
    // Start capturing output.
    ob_start(); 
    // Close PHP tags and start outputting HTML.
    ?>

    <div>
        <h1>HTML can go here</h1>
        <p>And also php code:</p>
        <p><?php echo 'Hello!'; ?></p>
    </div>
    <script>
        jQuery(document).ready(function() {
            alert('Message variable from PHP: <?php echo $message; ?>');
        });
    </script>

    <?php
    // Reopen PHP tags and return captured output.
    return ob_get_clean(); 
}
add_shortcode( 'wpse_279827_shortcode', 'wpse_279827_shortcode' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.