For one of my plugins I need to add a multiselect to my plugin options, but the options in the multiselect are dynamic (autogenerated from the given site's user roles).
$this->settings['manageraccess'] = array(
'section' => 'manager',
'title' => __('Manager Mode Access'),
'desc' => __(''),
'type' => 'manageraccess',
'std' => '',
'dflt' => '',
'helplink'=> 'yes',
'submit' => 'yes'
);
Normally I would add:
'choices' => array(
'op1' => 'Option 1',
'op2' => 'Option 2',
),
to the above array, but I need to generate them dynamically, like I do in my select box. My field case looks like this:
case 'manageraccess':
global $wp_roles;
$all_roles = $wp_roles->roles;
$editable_roles = apply_filters('editable_roles', $all_roles);
echo '<select multiple="multiple" class="select chozed-select' . $field_class . '" name="myplugin_options[' . $id . '][]" data-placeholder=" ">';
foreach($editable_roles as $role=>$theroles){
echo '<option value="' . esc_attr($role) . '" '.selected($options[$id], $role, false) . '>'.$wp_roles->role_names[$role].'</option>';
}
echo '</select>' . $helplink;
if ($desc != '')
echo '<br /><div class="ssfa-description">' . $desc . '</div>';
if ($submit != null)
echo '<br /><br /><br />'.$submit;
break;
Well I figured it out.
public function get_settings(){
global $wp_roles;
$this->settings['managertest'] = array(
'section' => 'manager',
'title' => __('Manager Mode Access'),
'desc' => __(''),
'type' => 'multi_select',
'std' => '',
'dflt' => '',
'choices' => $wp_roles->role_names,
'helplink'=> 'yes',
'submit' => 'yes'
);
Related
I need to add some custom field input type="file" to add a picture of product in Woocommerce?
You need the added picture to be displayed when viewing the product as the main photo.
This is the code I'm trying to make. But not enough knowledge.
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_field2s' );
function woo_add_custom_general_field2s() {
echo '<div class="options_group">';
woocommerce_wp_text_input( array( // Custom field
'id' => '_Button',
'label' => __( 'Choose file-code', 'woocommerce' ),
'placeholder' => '',
'type' => 'file',
'desc_tip' => 'true',
'custom_attributes' => array(),
'description' => __( 'Choose your pic of product', 'woocommerce' ),
) );
echo '</div>';
}
I have a cakedc search plugin with cakephp 3.0 working fine, but would like to have more advanced search filters like:
city = 'Los Angeles';
city != 'Los Angeles';
city LIKE '%Angeles%';
city LIKE 'Los%';
city NOT LIKE '%Angeles%';
city NOT LIKE 'Los%';
etc...
So i'm looking to add 2 dropdown select and 1 text input to achieve this.
'city' would be in a dropdown of db fields.
=, !=, like %?%, like %?, not like ?% conditions would be a dropdown
'los angeles' search value would be typed in.
Figured out, might not be perfect but seems to work fine:
View:
<?php echo $this->Form->input('dbfield', ['label' => 'Field', 'options' => ['' => '', 'region' => 'Region', 'city' => 'City', 'keyword' => 'Keyword']]); ?>
<?php echo $this->Form->input('dbconditions', [
'label' => 'Condition',
'options' => [
'' => '',
'contains' => 'contains',
'doesnotcontain' => 'does not contain',
'beginswith' => 'begins with',
'endswith' => 'ends with',
'isequalto' => 'is equal to',
'isnotequalto' => 'is not equal to',
],
]); ?>
<?php echo $this->Form->input('dbvalue', ['label' => 'Value', 'class' => 'form-control input-sm']); ?>
Model
public $filterArgs = [
'dbfield' => [
'type' => 'finder',
'finder' => 'conds0',
],
'dbconditions' => [
'type' => 'finder',
'finder' => 'conds0',
],
'dbvalue' => [
'type' => 'finder',
'finder' => 'conds',
],
];
public function findConds0(Query $query, $options = []) {
}
public function findConds(Query $query, $options = []) {
if($options['data']['dbconditions'] == 'contains') {
$conditions = [
$options['data']['dbfield'] . ' LIKE' => '%' . $options['data']['dbvalue'] . '%',
];
}
if($options['data']['dbconditions'] == 'doesnotcontain') {
$conditions = [
$options['data']['dbfield'] . ' NOT LIKE' => '%' . $options['data']['dbvalue'] . '%',
];
}
if($options['data']['dbconditions'] == 'beginswith') {
$conditions = [
$options['data']['dbfield'] . ' LIKE' => $options['data']['dbvalue'] . '%',
];
}
if($options['data']['dbconditions'] == 'endswith') {
$conditions = [
$options['data']['dbfield'] . ' LIKE' => '%' . $options['data']['dbvalue'],
];
}
if($options['data']['dbconditions'] == 'isequalto') {
$conditions = [
$options['data']['dbfield'] => $options['data']['dbvalue'],
];
}
if($options['data']['dbconditions'] == 'isnotequalto') {
$conditions = [
$options['data']['dbfield'] . ' != ' => $options['data']['dbvalue'],
];
}
return $query->where($conditions);
}
I clean the code, only beacuse look a big code.
In other way, why you include an empty option in the Condition select input? not look better with a default search condition?
Saludos
I am currently learning on WordPress plugin development and I am trying to build a short code for my plugin
my plugin is basically a custom post type called property which have 3 meta data boxs in it
price location and date of construction
and one taxonomy called property type which be setted at the backend with rent or sale
all of this works and if you put them in wordpress they will work
But my shortcode file does work properly the loop on the WP_Query should return all the post which I have made but instead its returning only the first element found in the WP_Query
Can anyone guide me or fix where I have mistaken please
thanks all
now my short code file name is: properties_post_type_shortcode.php
My plugin file name is: properties_post_type.php
code for properties_post_type_shortcode.php
<?php
add_shortcode('land_properties',function(){
$loop = new WP_Query(
array(
'post_type' => 'property_post',
'orderby' => 'title'
)
);
if ($loop->have_posts()){
$output = '<ul class="land_properties_list">';
$i=0;
while( $loop->have_posts() ){
$loop->the_post();
$meta=get_post_meta(get_the_id(),'' );
$output= '
<li>
<a href="' .get_permalink() . '">
' . get_the_title() . ' | '.
$meta['property_price'][0]. " " .
$meta['property_location'][0]. " " .
$meta['property_date'][0]. " " .
'
</a>
<div>' . get_the_excerpt() . '</div>
</li>
';
}
}
else {
$output="No lands added";
}
// $loop->wp_reset_postdata();
return $output;
});
code for properties_post_type.php
<?php
/*
* Plugin Name: properties_post_type
* Plugin URI: Have not be set yet
* Description: this plugin allow you to create custom post type property which you can be modified and edited
* Version: 1.0
* Author: Muhab Alwan
* Author URI: https://www.facebook.com/HaaaB
* License: A "Slug" license name e.g. GPL2
*/
class PROP_POST_TYPE{
//default contructor
public function __construct()
{
$this->register_post_type();
$this->taxonomies();
$this->metaboxes();
}
public function register_post_type()
{
$args= array(
'labels' => array(
'name'=> 'Properties',
'singular_value' => 'Property',
'add_new' => 'Add New Property',
'add_new_item' => 'Add New Property',
'edit_items' => 'Edit_Items',
'new_item' => ' Add New Items',
'view_item'=> 'View Item',
'search_items' => 'Search Items',
'not_found' => 'No Property Found',
'not_found_in_trash' => 'No Property Found In Trash'),
'query_var' =>'properties',
'rewrite' => array(
'slug' => 'property/'),
'public' => true,
'menu_position' => 80, // set postion in the backend menu
'menu_icon' => admin_url(). 'images/media-button-other.gif', // define an image for the prop
'supports' => array(
'title',
//'editor',
'excerpt',
//'custom-fields' when we need user to build their own meta box Not required in project
) // specify what wordpress types are custom post type support
);
register_post_type('property_post', $args );
}
public function taxonomies()
{
$taxonomies = array();
$taxonomies['property_type'] = array(
'hierarchical' => true,
'query_var' => 'movie_genere',
'rewrite' => array('slug' => 'prop/type'
),
'labels' => array(
'name'=> 'Properties Type',
'singular_value' => 'Property Type',
'add_new' => 'Add New Property Type',
'add_new_item' => 'Add New Property Type',
'edit_items' => 'Edit Properties Type',
'new_item' => ' Add New Properties Type',
'view_item'=> 'View Property Type',
'search_items' => 'Search Properties Type',
'popular_items' => 'Popular Properties Type',
'separate_items_with_comments' => 'Separate Property Type With Comments',
'add_or_remove_items' => 'Add Or Remove Properties Type',
'choose_from_most_used' => 'Choose From Most Used Properties Type'
)
);
$this-> register_all_taxonomies($taxonomies); // register all taxonomies build in this plugin
}
public function register_all_taxonomies($taxonomies)
{
// foreach is for registering many taxonomy
foreach ($taxonomies as $name=> $arr)
{
//register ( what the taxonomies name, array of the object type that we register ex post or page
register_taxonomy($name,array('property_post'),$arr );
}
}
public function metaboxes()
{
// FIRST PRICE META BOX
add_action('add_meta_boxes', function(){
//css id, title, cb func, page, priority level, call back func argum
add_meta_box('property_price','Property Price', 'property_price','property_post');
add_meta_box('property_location','Property Location', 'property_location','property_post');
add_meta_box('property_date','Date Of Construction', 'property_date','property_post');
});
//PRICE PROP
function property_price($post){
$price_length = get_post_meta($post->ID,'property_price', true);
?>
<p>
<label for="property_price"> :</label>
<input type="number" pattern="[0-9]+" size="50" class="widfat" name="property_price" id="property_price" value="<?php echo esc_attr($price_length)?>" /> </p>
<?php
}
add_action('save_post', function($id){
if ( isset ($_POST['property_price']))
{
update_post_meta(
$id,'property_price',
strip_tags($_POST['property_price'])
);
}
});
// LOCATIO PROP
function property_location($post){
$location_length = get_post_meta($post->ID,'property_location', true);
?>
<p>
<label for="property_location"> :</label>
<input type="text" class="widfat" name="property_location" id="property_location" value="<?php echo esc_attr($location_length)?>" /> </p>
<?php
}
add_action('save_post', function($id){
if ( isset ($_POST['property_location']))
{
update_post_meta(
$id,'property_location',
strip_tags($_POST['property_location'])
);
}
});
function property_date($post){
$dof_length = get_post_meta($post->ID,'property_date', true);
?>
<p>
<label for="property_date"> :</label>
<input type="date" class="widfat" name="property_date" id="property_date" value="<?php echo esc_attr($dof_length)?>" /> </p>
<?php
}
add_action('save_post', function($id){
if ( isset ($_POST['property_date']))
{
update_post_meta(
$id,'property_date',
strip_tags($_POST['property_date'])
);
}
});
//third date of construction metaboxes
}
}
// initialization essential to build a cust post
add_action('init', function(){
new PROP_POST_TYPE();
include dirname(__FILE__). '/properties_post_type_shortcode.php';
});
To query all posts, add the following to the args of WP_Query:
'posts_per_page' => -1
Your code:
$loop = new WP_Query( array(
'post_type' => 'property_post',
'orderby' => 'title',
'posts_per_page' => -1
) );
I'm looking for a way, to handle the Pagination-options in a dropdown menu.
I'd like my users to select the sorting order in the same form with my filtering options, so when they hit "Send", pagination-order is already set.orm
e.g. like:
$this->Form->input('paginationorder',array(
'label' => 'order',
'options' => array(
'sort:id/direction:desc' => 'New Items, desc',
'sort:id/direction:asc' => 'New Items,asc',
),
'div' => false
)
);
$(function() {
$('#sort-properties').change(function() { // replace the ID_OF_YOUR_SELECT_BOX with the id to your select box given by Cake
var price = $(this).val();
window.location = baseUrl + 'properties/property_view/'+price;
});
});
<?php
echo $this->Form->input('orderby', array(
'id' => 'sort-properties',
'options' => array(
'sort:sale_rent_price/direction:asc' => 'Sort by Price Low to High',
'sort:sale_rent_price/direction:desc' => 'Sort by Price High to Low',
'sort:created/direction:asc' => 'Sort by Date Old to New',
'sort:created/direction:desc' => 'Sort by Date New to Old'
),
'label' => false,
'empty' => 'Default Order'
));
?>
I would change the values of the options to e.g. fieldname.desc, like :
$this->Form->input('paginationorder',array(
'label' => 'order',
'options' => array(
'id.desc' => 'New Items, desc',
'id.asc' => 'New Items,asc',
),
'div' => false
)
);
Afterwards in the controller put the values to an array via the explode method:
$order = explode(".", $this->request->data['Model']['field']);
then you can use the values in your find condition, like:
$result = $this->Model->find('all', array(
'order' => array( $order[0] => $order[1] )
));
There is propably a more elegant way to make this, but this should work.
How can I create two radio buttons with one being preselected based on the value of $foo? The snippet below creates them fine but does not select either of the two buttons.
$options = array('standard' => ' Standard','pro' => ' Pro');
$attributes = array(
'legend' => false,
'value' => false,
'checked'=> ($foo == "pro") ? FALSE : TRUE,
);
echo $this->Form->radio('type',$options, $attributes);
It's simple.. use the default value to $foo:
$options = array(
'standard' => 'Standard',
'pro' => 'Pro'
);
$attributes = array(
'legend' => false,
'value' => $foo
);
echo $this->Form->radio('type', $options, $attributes);
As you can see on the documentation:
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::radio
you should preselect the value for any form field from the controller
#see http://www.dereuromark.de/2010/06/23/working-with-forms/ "Default Values"
This is the way to go
$attributes = array();
$options = array('standard' => 'Standard', 'pro' => 'Pro');
if($foo === 'pro') {
$attributes['default'] = 'pro';
}
echo $this->Form->radio('type', $options, $attributes);
A better Solution is to set the defaults in the controller as Mark has pointed. That way you can set defaults at the end of your controller's action like...
Let's assume your Model is Member with membership_type field
$this->data['Member']['membership_type '] = 'pro';
For CakePHP 3.x, the following syntax should work.
$options = array('Y'=>'Yes','N'=>'No');
$attributes = array('div' => 'input', 'type' => 'radio', 'options' => $options, 'default' => 'Y');
echo $this->Form->input('add to business directory',$attributes);
HTH