Create custom WordPress form and handle submission?

Standard
<?php

/**
 * Plugin Name: Custom Form
 * Plugin URI: http://https://tahiryasin.wordpress.com/
 * Description: Creates a custom form through shortcode and handles the submit event.
 * Version: 1.0
 * Author: Tahir Yasin
 * Author URI: https://tahiryasin.wordpress.com/about/
 * License: GPL2
 */

class CustomForm
{

    public function __construct()
    {
        add_action('init', array($this, 'init'));
        add_shortcode('custom_form', array($this, 'shortcode'));
    }

    public function init()
    {
        if (!empty($_POST['nonce_custom_form']))
        {
            if (!wp_verify_nonce($_POST['nonce_custom_form'], 'handle_custom_form'))
            {
                die('You are not authorized to perform this action.');
            } else
            {
                $error = null;
                if (empty($_POST['name']))
                {
                    $error = new WP_Error('empty_error', __('Please enter name.', 'tahiryasin'));
                    wp_die($error->get_error_message(), __('CustomForm Error', 'tahiryasin'));
                }
                else
                    die('Its safe to do further processing on submitted data.');
            }
        }
    }

    function shortcode($atts)
    {
        return "<form method='post' action=''>
    <input name='name' type='text' value='' />
    " . wp_nonce_field('handle_custom_form', 'nonce_custom_form') . "
        <input type='submit' value='Submit'/>
    </form>";
    }

}

$CustomForm = new CustomForm();
?>
Advertisements

WordPress advanced search filters based on custom fields

Standard
<?php
/**
 * Template Name: Search
 *
 * @package WordPress
 * @subpackage Twenty_Eleven
 * @since Twenty Eleven 1.0
 */
get_header();

$fields = array('country', 'state', 'zip', 'min_age', 'max_age');
foreach ($fields as $field)
{
    if ($_REQUEST[$field] != '')
    {
        if ($field == 'min_age')
        {
            $meta_query[] = array(
                'key' => $field,
                'value' => $_REQUEST[$field], // This is OK, WP_Query will sanitize input!
                'compare' => '>=',
            );
        } elseif ($field == 'max_age')
        {
            $meta_query[] = array(
                'key' => $field,
                'value' => $_REQUEST[$field], // This is OK, WP_Query will sanitize input!
                'compare' => '<=',
            );
        }
        else
        // We have something to match, otherwise ignore the field...
            $meta_query[] = array(
                'key' => $field,
                'value' => $_REQUEST[$field], // This is OK, WP_Query will sanitize input!
                'compare' => '=',
            );
    }
}
$args = array(
    'post_type' => 'vendor',
    'category__in' => $_REQUEST['category'],
    'posts_per_page' => -1, // -1 to display all results at once
    'order' => 'ASC',
    'meta_query' => $meta_query,
);
$query = new WP_Query($args);
?>
<div id="primary">
    <div id="content" role="main">
        <?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
                ?>
                <div class="post">
                    <?php the_title(); ?>
                </div>
                <?php
            endwhile; //end of the loop
        endif;
        ?>
    </div><!-- #content -->
</div><!-- #primary -->

<div id="secondary" class="widget-area" role="complementary">
    <form method="get">

        <label for="country">Country:</label>
        <input type="text" name="country" value="" />

        <label for="state">State: </label>
        <input type="text" name="state" value="" />

        <label for="zip">Zip: </label>
        <input type="text" name="zip" value="" />

        <label for="min_age">Min Age:</label>
        <input type="text" name="min_age" value="" />


        <label for="max_age">Max Age: </label>
        <input type="text" name="max_age" value="" />

        <label for="category">Category:</label><br />
        Category 1<input type="checkbox" value="5" name="category[]" /><br />
        Category 2<input type="checkbox" value="6" name="category[]" /><br />
        Category 3<input type="checkbox" value="7" name="category[]" /><br />
        Category 4<input type="checkbox" value="8" name="category[]" /><br />
        <input type="submit" value="Search" />
    </form>
</div>

<?php get_footer(); ?>

WP Custom Query for meta search

Standard
SELECT * FROM wp_posts p, wp_postmeta m, wp_terms t, wp_term_relationships r, wp_term_taxonomy tt 
WHERE p.post_type='your-post-type' 
AND p.ID=m.post_id 
AND p.id=r.object_id 
AND r.term_taxonomy_id=tt.term_taxonomy_id 
AND tt.term_id = t.term_id 
AND ( t.name LIKE '%90036%' OR p.post_title LIKE '%90036%' 
OR ( (meta_key='Address 1' AND meta_value LIKE '%90036%') 
OR (meta_key ='Address 2' AND meta_value LIKE '%90036%') 
OR (meta_key='State' AND meta_value LIKE '%90036%') 
OR (meta_key='City' AND meta_value LIKE '%90036%') 
OR (meta_key='Zip' AND meta_value LIKE '%90036%') ) ) 
GROUP BY ID 
ORDER BY post_title

Add remove WordPress dashboard widgets

Standard
    function example_remove_dashboard_widgets() {
    // Globalize the metaboxes array, this holds all the widgets for wp-admin
    global $wp_meta_boxes;
     
    // Remove the incomming links widget
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
     
    // Remove right now
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
    }
     
    // Hoook into the 'wp_dashboard_setup' action to register our function
    add_action('wp_dashboard_setup', 'example_remove_dashboard_widgets');
    add_action('wp_dashboard_setup', 'example_add_dashboard_widgets');
     
    function example_add_dashboard_widgets() {
    wp_add_dashboard_widget('example', 'Example', 'example_widget_callback');
    }
     
    function example_widget_callback() {
    echo 'testin widget';
    }