import css and javascript in wordpress plugins - wordpress

I have a wordpress plugin, I want to use colorbox in it, For this i need to import javascript, and css in the plugin.
Here is my plugin code.
<?php
/*
Plugin Name: My Plugin
Plugin URI: http://www.abc.com/
Description: abc
Author: abc
Version: 1.0
Author URI: http://www.abc.com
*/
function calc_form_markup() {
$markup = <<<EOT
<form>
<input type="text" name="author" id="author" value="{$author_default}" size="22" />
<label for="author"><small>Your name*</small></label><br>
<input type="text" name="email" id="email" value="{$email_default}" size="22" /> <label
for="email"><small>Your contact e-mail*</small></label><br>
<input type="text" name="subject" id="subject" value="" size="22" /> <label
for="subject"><small>Subject*</small></label><br>
<input name="btn" type="button" id="btn" value="Send" onclick="calc();" /><br>
</form>
EOT;
return $markup;
}
add_shortcode('calc_form', 'calc_form_markup');
function calc_form_js() { ?>
<script type="text/javascript">
function calc()
{
alert ("My javascript function executes successfully...");
}
</script>
<?php }
add_action('wp_head', 'calc_form_js');
?>
And i want to import following files in the above plugin.
<link rel="stylesheet" href="colorbox.css" />
<script src="jquery.min.js"></script>
<script src="colorbox.js"></script>
Thanks in advance

Check on the codex the functions wp_register_script and wp_enqueue_script
http://codex.wordpress.org/Function_Reference/wp_enqueue_script
for the css files , it is the same system
http://codex.wordpress.org/Function_Reference/wp_enqueue_style

Related

woocommerce search only parent category products based on body class

I am using the code below but am not sure how to add in two separate search forms that search for products ONLY in the parent category. Either product_parent_cat_floral or product_parent_cat_rentals
<?php
$classes = get_body_class();
if (in_array('product_parent_cat_rentals',$classes)) {
?>
//rentals search form would go here
<?php } else { ?>
//flowers search form would go here
<?php } ?>
Can I edit this search form to somehow make it only search for products within the appropiate parent class?
<form role="search" method="get" id="searchform" action="http://botanicaevents.com/rentals/">
<div>
<label class="screen-reader-text" for="s">Search for:</label>
<input type="text" value="" name="s" id="s" placeholder="Search for products" />
<input type="submit" id="searchsubmit" value="Search" />
<input type="hidden" name="post_type" value="product" />
</div>
</form>
I think you can simply use:
<input type="hidden" name="product_cat" value="rentals" />
so your search query becomes:
?s=some+search+string&post_type=product&product_cat=rentals
to additionally restrict the search within the rentals product category.

Saving radio button options to access them later in wordpress plugin

This is my code for the radio buttons. I would like them to be displayed in the settings page and save it for future use.
<form method="post" action="options.php">
<?php
wp_nonce_field('update-options');
$rsp_position = get_option('rsp_position');
if(empty($rsp_position)){
$rsp_position = "true";
} else {
$rsp_position = get_option('rsp_position');
}
?>
<p><strong>Ad Placing:</strong><br />
<input type="radio" id="1" name="rsp_position" <?php if($rsp_position == 'true') echo 'checked="checked"'; ?> value="true" />yes <br />
<input type="radio" id="2" name="rsp_position" <?php if($rsp_position == 'false') echo 'checked="checked"'; ?> value="flase" />no <br />
<input type="text" name="publisher-id" size="45" value="<?php echo get_option('publisher-id'); ?>" />
</p>
<p><input type="submit" name="Submit" value="Save" /></p>
<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="publisher-id" />
<?php
settings_fields( 'save_position' );
register_setting( 'save_position', 'rsp_position');
?>
You're going along the right lines, but your calls to settings_fields and register_setting aren't in the right place.
Read Otto's tutorial on this, it's a good introduction to the settings API.
Basically, add an admin_init action and in the callback register the setting and use add_settings_field to create the form items.
Then in your add_options_page callback you make the call to settings_fields to display the entries created with add_settings_field.
If you're still struggling having worked through Otto's tutorial, post back again.
EDIT: Following pastebin entry.
The failure to save is a result of your validation function not returning the expected value. The preg_match is not finding the right value and turning the string to '' (nothing) before saving - so it looks like it's not saving, when in fact it's saving nothing.
You could also check out the following functions which may be useful.
submit_button
settings_errors

how to submit a form in wordpress

I am using wordpress 3.3.1 with twentyten theme,
i have created a plugin to create a custom form,
i have successfully installed this in wordpress,
my plugin file code is as follows
<?php
function guest_event_form()
{
if(isset($_POST['submit']) and $_POST['action']=='new registration')
{
global $wpdb;
$wpdb->query("Insert Query...");
}
else
{
?>
<form method="POST" action="" name="guest_registration" enctype="multipart/form-data">
<input type="text" id="name" name="name" value="">
<input type="submit" name="submit" value="Register Me Now"/>
<input type="hidden" name="action" value="new registration" />
</form>
<?php
}
}
add_shortcode( 'guest_event_form', 'guest_event_form' );
?>
whenever i am submitting this form, i returns to same page with search results,
so i guess the problem whenever i submit this form, wordpress takes this submit action as a search action, and it starts search
how do i overcome this problem??
The Problem is because of following form element's id
<input type="text" id="name" name="name" value="">
name is one of the wordpress internal variable
Change it like this:
<input type="text" id="customername" name="customername" value="">

How to implement a search feature for WordPress CMS?

I need to incorporate a "Search" feature within my WordPress CMS site that I am currently developing and was hoping to attach this feature/plugin to the following piece of code and unsure how to do this in WordPress, i.e.:
<div id="search_box">
<form method="get" action="/search" id="form">
<input name="white_box" type="text" class="search" value="Search site" size="19" maxlength="80" id="white_box" onfocus="if (this.value=='Search site') this.value = ''"/>
<input name="submit" type="image" class="submit" value="submit" src="images/search_btn.jpg" />
</form>
</div>
I basically want to incorporate a "Search" feature on my site.
#Tonsils you may use any of below ready-to-use WordPress plugins depends on your requirement...
Use Relevanssi Plugin for Multilingual Support
http://wordpress.org/extend/plugins/relevanssi/
Use WPSearch for well behave custom Search
http://wordpress.org/extend/plugins/wpsearch/
Use Search-Everything for all type of Content Search
http://wordpress.org/extend/plugins/search-everything/
This a very basic WordPress search form. See the difference in the form action URL?
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<input type="text" size="14" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" class="s" />
<input type="submit" id="searchsubmit" value="<?php _e('GO'); ?>" />
</form>

Wordpress adding custom field update fails

i am picking up wordpress development and reading the book digging into wordpress. i have the code below in functions.php
<?php
add_action('admin_menu', 'addAmazonAffiliateOptions');
function addAmazonAffiliateOptions() {
add_options_page('Global Custom Fields', 'Global Custom Fields', 8, 'functions', 'editGlobalCustomFields');
}
function editGlobalCustomFields() { ?>
<div class="wrap">
<h2>Global Custom Fields</h2>
<form action="options.php" method="post">
<?php wp_nonce_field('update_options'); ?>
<p>
<label for="amazonId"><strong>Amazon ID</strong></label>
<input type="text" name="amazonId" value="<?php echo get_option('amazonId'); ?>" />
</p>
<p>
<input type="submit" name="submit" value="Update Options" />
<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="amazonId" />
</p>
</form>
</div>
<?php }
when i try to save the options, i get
Your attempt to edit your settings has
failed.
how do i debug such things in wordpress?
I think you need to be using register_setting() - the WordPress core and API has changed quite considerably since that book was published.
Check out the codex on WordPress 2.7+ for adding plugin options pages.

Resources