https://wordpress.org/plugins/cf7-conditional-fields/
Showing posts with label form. Show all posts
Showing posts with label form. Show all posts
Thursday, June 22, 2017
Friday, December 30, 2016
add input field in wp admin and Global Options Page and custom form in wp admin
http://stackoverflow.com/questions/11715465/how-do-i-create-a-universal-custom-field-in-wordpress
/** EGR Magnificent config in wp-admin */
add_action( 'admin_menu', 'my_plugin_menu' );
function my_plugin_menu() {
add_options_page( 'Magnificent', 'Magnificent API', 'manage_options', 'magnificent-config', 'EGR_magnificent_options' );
}
function EGR_magnificent_options() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
echo '<div class="wrap">';
echo '<p>EGR Magnificent API configuration.</p><form method="post" action="options.php">';
wp_nonce_field('update-options');
echo ' <p><strong>JWT token secret key:</strong><br />
<input type="text" name="jwtsecret" size="45" value="'; echo get_option('jwtsecret');
echo '" />
<br /><strong>JWT token expiry time (in minutes):</strong><br />
<input type="number" name="jwtexpire" size="45" value="'; echo get_option('jwtexpire');
echo '" />
</p>
<p><input type="submit" name="Submit" value="Store Options" /></p>
<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="jwtsecret, jwtexpire" />
</form>';
echo '</div>';
}
/** EGR Magnificent config in wp-admin */
add_action( 'admin_menu', 'my_plugin_menu' );
function my_plugin_menu() {
add_options_page( 'Magnificent', 'Magnificent API', 'manage_options', 'magnificent-config', 'EGR_magnificent_options' );
}
function EGR_magnificent_options() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
echo '<div class="wrap">';
echo '<p>EGR Magnificent API configuration.</p><form method="post" action="options.php">';
wp_nonce_field('update-options');
echo ' <p><strong>JWT token secret key:</strong><br />
<input type="text" name="jwtsecret" size="45" value="'; echo get_option('jwtsecret');
echo '" />
<br /><strong>JWT token expiry time (in minutes):</strong><br />
<input type="number" name="jwtexpire" size="45" value="'; echo get_option('jwtexpire');
echo '" />
</p>
<p><input type="submit" name="Submit" value="Store Options" /></p>
<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="jwtsecret, jwtexpire" />
</form>';
echo '</div>';
}
Friday, October 28, 2016
Add custom taxonomy type under post with form
REF : https://metabox.io/plugins/custom-taxonomy/
// Post Stock box
add_action( 'add_meta_boxes', 'post_stock' );
function post_stock() {
add_meta_box(
'post_stock',
__( 'Stock', 'agrg' ),
'post_stock_content',
'post',
'side',
'high'
);
}
function post_stock_content( $post ) {
wp_nonce_field( 'myplugin_meta_boxee', 'myplugin_meta_box_nonceee' );
$post_stock = get_post_meta( $post->ID, 'post_stock', true );
echo '<label for="post_stock"></label>';
echo '<input type="text" id="post_stock" name="post_stock" placeholder="'._e('Enter Stock here','agrg').'" value="';
echo $post_stock;
echo '">';
}
add_action( 'save_post', 'post_stock_save' );
function post_stock_save( $post_id ) {
global $post_stock;
if ( ! isset( $_POST['myplugin_meta_box_nonceee'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonceee'], 'myplugin_meta_boxee' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if(isset($_POST["post_stock"]))
$post_stock = $_POST['post_stock'];
update_post_meta( $post_id, 'post_stock', $post_stock );
}
$stock_labels = array(
'name' => __( 'Stock', 'agrg' ),
'singular_name' => __( 'Stock', 'agrg' ),
'search_items' => __( 'Search Stocks', 'agrg' ),
'popular_items' => __( 'Popular Stocks', 'agrg' ),
'all_items' => __( 'All Stocks', 'agrg' ),
'parent_item' => __( 'Parent Stock', 'agrg' ),
'parent_item_colon' => __( 'Parent Stock:', 'agrg' ),
'edit_item' => __( 'Edit Stock', 'agrg' ),
'update_item' => __( 'Update Stock', 'agrg' ),
'add_new_item' => __( 'Add New Stock', 'agrg' ),
'new_item_name' => __( 'New Stock Name', 'agrg' ),
'separate_items_with_commas' => __( 'Separate Stocks with commas', 'agrg' ),
'add_or_remove_items' => __( 'Add or remove Stock', 'agrg' ),
'choose_from_most_used' => __( 'Choose from the most used Stocks', 'agrg' ),
'menu_name' => __( 'Stocks', 'agrg' )
);
register_taxonomy("Stock",
array("post"),
array("hierarchical" => true,
'labels' => $stock_labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array('slug' => __('Stock', 'agrg'))
)
);
Add value in custom taxonomy pro-grammatically :
REF: https://codex.wordpress.org/Function_Reference/wp_insert_term
/* Location update while seller registration */
$saddress = sanitize_text_field($address);
//check location is exist
$loc_exist = term_exists( $saddress ); // array is returned if taxonomy is given
if (empty($loc_exist) ) {
$parent_term = term_exists( 'location' ); // array is returned if taxonomy is given
$parent_term_id = $parent_term['term_id']; // get numeric term id
wp_insert_term(
$saddress, // the term
'location', // the taxonomy
array(
'description'=> 'A seller location.',
'slug' => $saddress,
'parent'=> $parent_term_id
)
);
}
// Post Stock box
add_action( 'add_meta_boxes', 'post_stock' );
function post_stock() {
add_meta_box(
'post_stock',
__( 'Stock', 'agrg' ),
'post_stock_content',
'post',
'side',
'high'
);
}
function post_stock_content( $post ) {
wp_nonce_field( 'myplugin_meta_boxee', 'myplugin_meta_box_nonceee' );
$post_stock = get_post_meta( $post->ID, 'post_stock', true );
echo '<label for="post_stock"></label>';
echo '<input type="text" id="post_stock" name="post_stock" placeholder="'._e('Enter Stock here','agrg').'" value="';
echo $post_stock;
echo '">';
}
add_action( 'save_post', 'post_stock_save' );
function post_stock_save( $post_id ) {
global $post_stock;
if ( ! isset( $_POST['myplugin_meta_box_nonceee'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonceee'], 'myplugin_meta_boxee' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if(isset($_POST["post_stock"]))
$post_stock = $_POST['post_stock'];
update_post_meta( $post_id, 'post_stock', $post_stock );
}
$stock_labels = array(
'name' => __( 'Stock', 'agrg' ),
'singular_name' => __( 'Stock', 'agrg' ),
'search_items' => __( 'Search Stocks', 'agrg' ),
'popular_items' => __( 'Popular Stocks', 'agrg' ),
'all_items' => __( 'All Stocks', 'agrg' ),
'parent_item' => __( 'Parent Stock', 'agrg' ),
'parent_item_colon' => __( 'Parent Stock:', 'agrg' ),
'edit_item' => __( 'Edit Stock', 'agrg' ),
'update_item' => __( 'Update Stock', 'agrg' ),
'add_new_item' => __( 'Add New Stock', 'agrg' ),
'new_item_name' => __( 'New Stock Name', 'agrg' ),
'separate_items_with_commas' => __( 'Separate Stocks with commas', 'agrg' ),
'add_or_remove_items' => __( 'Add or remove Stock', 'agrg' ),
'choose_from_most_used' => __( 'Choose from the most used Stocks', 'agrg' ),
'menu_name' => __( 'Stocks', 'agrg' )
);
register_taxonomy("Stock",
array("post"),
array("hierarchical" => true,
'labels' => $stock_labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array('slug' => __('Stock', 'agrg'))
)
);
Add value in custom taxonomy pro-grammatically :
REF: https://codex.wordpress.org/Function_Reference/wp_insert_term
/* Location update while seller registration */
$saddress = sanitize_text_field($address);
//check location is exist
$loc_exist = term_exists( $saddress ); // array is returned if taxonomy is given
if (empty($loc_exist) ) {
$parent_term = term_exists( 'location' ); // array is returned if taxonomy is given
$parent_term_id = $parent_term['term_id']; // get numeric term id
wp_insert_term(
$saddress, // the term
'location', // the taxonomy
array(
'description'=> 'A seller location.',
'slug' => $saddress,
'parent'=> $parent_term_id
)
);
}
Wednesday, October 12, 2016
Custom validation in contact form 7
function.php :
/** text validation for contact form 7 **/
add_filter( 'wpcf7_validate_text*', 'custom_text_confirmation_validation_filter', 20, 2 );
function custom_text_confirmation_validation_filter( $result, $tag ) {
$tag = new WPCF7_Shortcode( $tag );
if ( 'your-name' == $tag->name ) {
$your_name = isset( $_POST['your-name'] ) ? trim( $_POST['your-name'] ) : '';
//remove space from string
$trim_name = str_replace(' ', '', $your_name);
//special char validation
if ( $your_name ) {
if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $your_name) || ! ctype_alpha($trim_name) )
{
$result->invalidate( $tag, "Name seems invalid." );
}
}
}
return $result;
}
/** URL validation for contact form 7 **/
function cf7_custom_url_check( $result, $url )
{
if ($result)
{
$regex='@^(https?\://)?(www\.)?([a-z0-9]([a-z0-9]|(\-[a-z0-9]))*\.)+[a-z]+$@i';
if (!preg_match($regex,$url)) $result=FALSE;
}
return $result;
}
add_filter( 'wpcf7_is_url', 'cf7_custom_url_check', 10, 2 );
/** text validation for contact form 7 **/
add_filter( 'wpcf7_validate_text*', 'custom_text_confirmation_validation_filter', 20, 2 );
function custom_text_confirmation_validation_filter( $result, $tag ) {
$tag = new WPCF7_Shortcode( $tag );
if ( 'your-name' == $tag->name ) {
$your_name = isset( $_POST['your-name'] ) ? trim( $_POST['your-name'] ) : '';
//remove space from string
$trim_name = str_replace(' ', '', $your_name);
//special char validation
if ( $your_name ) {
if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $your_name) || ! ctype_alpha($trim_name) )
{
$result->invalidate( $tag, "Name seems invalid." );
}
}
}
return $result;
}
/** URL validation for contact form 7 **/
function cf7_custom_url_check( $result, $url )
{
if ($result)
{
$regex='@^(https?\://)?(www\.)?([a-z0-9]([a-z0-9]|(\-[a-z0-9]))*\.)+[a-z]+$@i';
if (!preg_match($regex,$url)) $result=FALSE;
}
return $result;
}
add_filter( 'wpcf7_is_url', 'cf7_custom_url_check', 10, 2 );
Subscribe to:
Posts (Atom)