I am trying to: apply mask custom fields
first insert the function in my theme for js maskedinput as code
[php]function mascara_cadastro(){
is_singular('cpt')
wp_enqueue_script('masked-input', 'hidden link'.'/js/jquery.maskedinput.min.js', array('jquery'));
}
add_action('wp_enqueue_scripts', 'mascara_cadastro');</ code>
Note that I used is_singular to determine the created cpt
after entering the function to activate the mask in the custom field tool set "WPCF-field" as code
[php]function activate_masked_input(){
is_singular('cpt') ){
?>
<script type="text/javascript">
jQuery( function($){
$("#wpcf-field").mask("99/99/9999");
});
</script>
<?php
}
}
add_action('wp_footer', 'activate_masked_input');</ code>
and the function does not work, as much googled to find a solution and nothing could please help me
tks
Dear marcelo,
There is an error in your PHP codes, I suggest you modify it from:
function mascara_cadastro(){
is_singular('cpt')
wp_enqueue_script('masked-input', '<em><u>hidden link</u></em>'.'/js/jquery.maskedinput.min.js', array('jquery'));
}
add_action('wp_enqueue_scripts', 'mascara_cadastro');
To:
function mascara_cadastro(){
if(is_singular('cpt')){
wp_enqueue_script('masked-input', '<em><u>hidden link</u></em>'.'/js/jquery.maskedinput.min.js', array('jquery'));
}
}
add_action('wp_enqueue_scripts', 'mascara_cadastro');
More help:
hidden link
the changes made and was as follows:
function mascara_cadastro(){
if(is_singular('imovel')){
wp_enqueue_script('masked-input', '<em><u>hidden link</u></em>'.'/js/jquery.maskedinput.min.js', array('jquery'));
}
}
add_action('wp_enqueue_scripts', 'mascara_cadastro');
function activate_masked_input(){
if(is_singular('imovel') ){
?>
<script type="text/javascript">
jQuery( function($){
$("wpcf-preco-de-venda-ou-locacao").mask("999.999");
});
</script>
<?php
}
}
add_action('wp_footer', 'activate_masked_input');
but still does not work, I'm trying to get the mask appear when you insert a new post on the backend, /wp-admin/post-new.php?post_type=cpt, is it necessary to make any other statement?
It is in wordpress admin side, I suggest you try with the wordpress action hook "admin_enqueue_scripts", for example,
1) create a JS file "masked_input.js" in same folder as file jquery.maskedinput.min.js, with below codes:
jQuery(document).ready(function($){
$('input[data-wpt-id="wpcf-preco-de-venda-ou-locacao"]').mask("999.999");
});
2) modify the PHP codes as below:
function mascara_cadastro(){
$current_screen = get_current_screen();
if( isset($current_screen->post_type) && $current_screen->post_type == 'some-cpt'){
wp_enqueue_script('masked-input', '<em><u>hidden link</u></em>'.'/js/jquery.maskedinput.min.js', array('jquery'));
wp_enqueue_script('masked_input-js', '<em><u>hidden link</u></em>'.'/js/masked_input.js', array('jquery'));
}
}
add_action('admin_enqueue_scripts', 'mascara_cadastro');
Perfect
I am very grateful to you Luo Yang
one more question, if I want to apply in more than one cpt I use array ( 'CPT1', 'CPT2', 'cpt3')?
Yes, you can modify the PHP codes as below:
function mascara_cadastro(){
$current_screen = get_current_screen();
if( isset($current_screen->post_type)
&& in_array($current_screen->post_type, array('some-cpt', 'some-cpt2', 'some-cpt3')){
wp_enqueue_script('masked-input', '<em><u>hidden link</u></em>'.'/js/jquery.maskedinput.min.js', array('jquery'));
wp_enqueue_script('masked_input-js', '<em><u>hidden link</u></em>'.'/js/masked_input.js', array('jquery'));
}
}
add_action('admin_enqueue_scripts', 'mascara_cadastro');
please replace 'some-cpt', 'some-cpt2', 'some-cpt3' with the custom post type slugs.
More help:
hidden link
Luo I thank you very much. Sorry to extend the question, all you gave me so far worked perfectly.
My only problem is that the masks are being recorded in the database, which believe not ideal due to the use of data in research, filters, sort order, etc.
I questioned the developer jquery script about it, he informed me that the plugin is not responsible for recording the data with mask in the database.
You would have some light on this issue?
Thank you again.
Since the original problem has been resolved, please create another thread for the new question. this will help other users to find the answers.
And you will need describe more details for the questions:
What are the masks being recorded in the database? what do you want the value to be?
How do you setup the custom field? is it a custom single line field?
we need details to duplicate same problem, thanks