Suporte » Plugins » Ler javascript dentro plugin

  • Resolvido Rgiz

    (@rgiz)


    Ola pessoal, estou tentando colocar um pequeno jquery que limita o número em 150 caracteres digitados dentro da text box da mensagem de um plugin de contato. Versão do meu WP é: 3.3.1

    Mas não estou conseguindo que ele funcione, segue abaixo o código referente ao plugin de contato. No código abaixo, pode perceber que existe código do jquery, porem não esta funcionando de nenhum jeito..

    <?php
    /*
    Plugin Name: xx
    Description: xx
    */
    
    if ( !is_admin() ) {
       wp_register_script('jquery.limit',
           get_bloginfo('template_directory') . '/js/jquery.limit.js',
           array('jquery'),
           '1.0' );
       // pone en cola es script
       wp_enqueue_script('jquery.limit');
    } 
    
    // We need some CSS to position the paragraph
    function limitwords_css() {
    	// This makes sure that the posinioning is also good for right-to-left languages
    
    	echo "
    	<style type='text/css'>
    	#email {
    		color: #e50671;
    		text-decoration: underline;
    	}
    	</style>
    	";
    }
    
    function contact_form_markup() {
    
    $form_action    = get_permalink();
    $author_default = $_COOKIE['comment_author_'.COOKIEHASH];
    $email_default  = $_COOKIE['comment_author_email_'.COOKIEHASH];
    
    if ( ($_SESSION['contact_form_success']) ) {
    $contact_form_success = '<p style="color: green"><strong>Thank you for your inquiry. We will contact you shortly to answer your questions.</strong></p>';
    unset($_SESSION['contact_form_success']);
    }
    
    $markup = <<<EOT
    
    <div id="commentform"><h3>Contact Form</h3>
    
    	{$contact_form_success}
    
    <!-- limit words -->
    <script  type="text/javascript">
    $(document).ready(function() {
      $("#message").limita({
    	limit: 150,
    	id_result: "counter",
    	alertClass: "alert"
    });
    	});
    </script>
    <!-- limit words -->
    
       <form onsubmit="return validateForm(this);" action="{$form_action}" method="post" enctype="multipart/form-data" style="text-align: left">
    
       <p><input type="text" name="author" id="author" value="{$author_default}" size="22" /> <label for="author"><small>Your name*</small></label></p>
    
       <p><input type="text" name="email" id="email" value="{$email_default}" size="22" /> <label for="email"><small>Your contact e-mail*</small></label></p>
    
       <p><input type="text" name="subject" id="subject" value="" size="22" /> <label for="subject"><small>Subject*</small></label></p>
    
       <p><textarea name="message" id="message" cols="100%" rows="10">type your message here...</textarea></p>
       <p><div id="counter">contador</div></p>
    
       <p><label for="attachment"><small>Attachment</small></label> <input type="file" name="attachment" id="attachment" /></p>
       <p><input name="send" type="submit" id="send" value="Send" /></p>
    
       <input type="hidden" name="contact_form_submitted" value="1">
    
       </form>
    
    </div>
    
    EOT;
    
    return $markup;
    
    }
    
    add_shortcode('contact_form', 'contact_form_markup');
    
    function contact_form_process() {
    
    session_start();
    
     if ( !isset($_POST['contact_form_submitted']) ) return;
    
     $author  = ( isset($_POST['author']) )  ? trim(strip_tags($_POST['author'])) : null;
     $email   = ( isset($_POST['email']) )   ? trim(strip_tags($_POST['email'])) : null;
     $subject = ( isset($_POST['subject']) ) ? trim(strip_tags($_POST['subject'])) : null;
     $message = ( isset($_POST['message']) ) ? trim(strip_tags($_POST['message'])) : null;
    
     if ( $author == '' ) wp_die('Error: please fill the required field (name).');
     if ( !is_email($email) ) wp_die('Error: please enter a valid email address.');
     if ( $subject == '' ) wp_die('Error: please fill the required field (subject).');
    
     //we will add e-mail sending support here soon
    
    require_once ABSPATH . WPINC . '/class-phpmailer.php';
    $mail_to_send = new PHPMailer();
    
    $mail_to_send->FromName = $author;
    $mail_to_send->From     = $email;
    $mail_to_send->Subject  = $subject;
    $mail_to_send->Body     = $message;
    
    $mail_to_send->AddReplyTo($email);
    $mail_to_send->AddAddress('[E-mail removido por um moderador]'); //contact form destination e-mail
    
    if ( !$_FILES['attachment']['error'] == 4 ) { //something was send
    
    	if ( $_FILES['attachment']['error'] == 0 && is_uploaded_file($_FILES['attachment']['tmp_name']) )
    
    		$mail_to_send->AddAttachment($_FILES['attachment']['tmp_name'], $_FILES['attachment']['name']);
    
    	else 
    
    		wp_die('Error: there was a problem with the file upload. Try again later.');
    
    }
    
    if ( !$mail_to_send->Send() ) wp_die('Error: unable to send e-mail - status code: ' . $mail_to_send->ErrorInfo);
    
    $_SESSION['contact_form_success'] = 1;
    
     header('Location: ' . $_SERVER['HTTP_REFERER']);
     exit();
    
    } 
    
    add_action('init', 'contact_form_process');
    
    function contact_form_js() { ?>
    
    <script type="text/javascript">
    function validateForm(form) {
    
    	var errors = '';
    	var regexpEmail = /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/;
    
    	if (!form.author.value) errors += "Error: please fill the required field (name).\n";
    	if (!regexpEmail.test(form.email.value)) errors += "Error: please enter a valid email address.\n";
    	if (!form.subject.value) errors += "Error: please fill the required field (subject).\n";
    
    	if (errors != '') {
    		alert(errors);
    		return false;
    	}
    
    return true;
    
    }
    </script>
    
    <?php }
    
    add_action('wp_head', 'contact_form_js');
    
    ?>

    E aqui a composição do pequeno jquery limitador.

    <script  type="text/javascript">
    $(document).ready(function() {
      $("#message").limita({
    	limit: 150,
    	id_result: "counter",
    	alertClass: "alert"
    });
    	});
    </script>

    Alem de ter um jquery.limit.js

    Obrigado pela atençao pessoal.

Visualizando 1 resposta (de um total de 1)
  • Rgiz, cheque duas coisas:

    o script está sendo carregado? Estou vendo que vc tá usando a notação mínima em

    wp_enqueue_script('jquery.limit');

    então é bom conferir.

    Depois, use troque os $ por jQuery, em:

    <script  type="text/javascript">
    jQuery(document).ready(function() {
      jQuery("#message").limita({
    	limit: 150,
    	id_result: "counter",
    	alertClass: "alert"
    });
    	});
    </script>

Visualizando 1 resposta (de um total de 1)
  • O tópico ‘Ler javascript dentro plugin’ está fechado para novas respostas.