Add a searchbox to a dropdown Wordpress - html

I'm using Woocommerce and have edited the Towns/Cities checkout form to include a dropdown of 500 towns and cities that I need users to choose from. How do I implement a searchbox, so that they are able to search for their town?
Basically, how do I create something like this:
for this:
I'm customising that field in Woocommerce, using the following code
add_filter( 'woocommerce_default_address_fields' , 'customize_checkout_city_field' );
function customize_checkout_city_field( $address_fields ) {
// Set HERE the cities (one line by city)
$towns_cities_arr = array(
'0' => __('Select your city', 'my_theme_slug'),
'paris' => 'Paris',
'versailles' => 'Versailles',
'cannes' => 'Cannes',
);
// Customizing 'billing_city' field
$address_fields['city']['type'] = 'select';
$address_fields['city']['class'] = array('form-row-last', 'my-custom-class'); // your class here
$address_fields['city']['label'] = __('Town / city', 'my_theme_slug');
$address_fields['city']['options'] = $towns_cities_arr;
// Returning Checkout customized fields
return $address_fields;
}

This plugin you want to use is Select2
https://select2.github.io/examples.html
You already have the custom class on your field called my-custom-class
All you need to do is import the select2 libs. You can do this in your theme functions.php.
function mytheme_enqueue_custom_scripts() {
wp_enqueue_style( 'select2-css', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css' );
wp_enqueue_script( 'select2-js', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js' );
}
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_custom_scripts' );
And then add some jQuery to initialize it.
<script type="text/javascript">
jQuery(document).ready(function( $ ) {
$(".my-custom-class").select2();
});
</script>

Related

Modify footer in MediaWiki

is there a way to put an external link on MediaWiki's footer?
I've tried to add in my localsetting.php this code:
$wgHooks['SkinTemplateOutputPageBeforeExec'][] = function( $sk, &$tpl ) {
$tpl->set( 'aboutus', $sk->footerLink( 'aboutus', 'aboutuspage' ) );
// or to add non-link text:
$tpl->set( 'footertext', 'Text to show in footer' );
$tpl->data['footerlinks']['places'][] = 'aboutus';
return true;
};
But this go to create a Mediawiki's page, and i want to add a link to a contact page. I've modified the MediaWiki:Aboutus and MediaWiki:Aboutuspage pages, but nothing, continue to remind me to a MediaWiki's page.
What do you put in "MediaWiki:Aboutus"? You've to actually create the html link.
Try this
$wgHooks['SkinTemplateOutputPageBeforeExec'][] = function( $sk, &$tpl ) {
$aboutusLink = Html::element( 'a', [ 'href' => $sk->msg( 'aboutus-url' )->escaped() ],
$sk->msg( 'aboutus-label' )->text() ) ;
$tpl->set( 'aboutus', $aboutusLink );
$tpl->data['footerlinks']['places'][] = 'aboutus';
return true;
};
Then on your wiki create the page "MediaWiki:Aboutus-url" and directly place the external link you want you use. Also create the page "MediaWiki:Aboutus-label" and place the text to show on the surface. This allows you to change the url and label by modifying that wiki page messages any time, just as it seems you wanted to do so. If you want to hardcode everything in LocalSettings.php however, then use this:
$wgHooks['SkinTemplateOutputPageBeforeExec'][] = function( $sk, &$tpl ) {
$aboutusLink = Html::rawelement( 'a', [ 'href' => 'https://stackoverflow.com/questions/59427663/modify-footer-in-mediawiki' ],
'Text to show as label' );
$tpl->set( 'aboutus', $aboutusLink );
$tpl->data['footerlinks']['places'][] = 'aboutus';
return true;
};

Wordpress: programmatically create sub menu for frontend

I'd like to write a wordpress-plugin, where you can add a page.
If you submit the page it should also create a submenu under the menu "Teams".
Until now I can create a page through my code, but not the submenu.
I tried different functions I found on google, but nothing will work.
Does anyone know a function or a script that will help?
Yes sure, use the following as a sample to get you going. The clause to check if you are in the right menu may need altering or deleting if you don't have multiple menu objects defined.
menu_item_parent is vital and that is the parent item uid. find that by viewing your front end source code. You should find that each menu item inserted via WP menu creating functions inserts the unique items id.
// add a sub menu dynamically via code!
function aj_add_menu_item( $items, $args ) {
// check we are in the right menu
if( $args -> theme_location =="primary" ) {
$new_links = array();
// Create a nav_menu_item object
$newItem = array(
'title' => "Offers",
'menu_item_parent' => 71,
'ID' => 'loginout',
'db_id' => '12312332', // something random
'url' => "offers",
'classes' => array( 'menu-item' )
);
$items[] = (object) $newItem; // add to end of existing object.
menu_item_parent value will ensure it goes in right place
return $items;
}else{
return $items;
}
}
add_filter( 'wp_nav_menu_objects', 'aj_add_menu_item', 10, 2 );

Filter custom post type by category in REST API v2 in WordPress

I am creating an Android app using the WP API. I managed to show a custom post type in the REST API which can be viewed in:
http://localhost/wordpress/wp-json/wp/v2/property
Now I want to filter properties by their category, e.g. villa, home, rent, etc. I have tried the following, but it does not work:
http://localhost/wordpress/wp-json/wp/v2/property?filter[category_name]=villa`
http://localhost/wordpress/wp-json/wp/v2/property?filter[category]=apartment`
I was running into this issue as well with my custom post types.
You need to query by your custom taxonomy (eg. property_categories, or whatever you named it in your register_taxonomy() function) and then by the term.
&filter[taxonomy]=property_categories&filter[term]=villa
This saved my own day
mysite.com/wp-json/wp/v2/property?property-status=33
then this was my call in my themes functions file
//get custom post types in the WP-API v2
//for mobile app
add_filter( 'register_post_type_args', 'sb_add_cpts_to_api', 10, 2 );
function sb_add_cpts_to_api( $args, $post_type ) {
if ( 'property' === $post_type ) {
$args['show_in_rest'] = true;
}
return $args;
}
//to add taxonomy for properties to API result
add_action( 'init', 'sb_add_taxes_to_api', 30 );
function sb_add_taxes_to_api() {
$taxonomies = get_taxonomies( 'property-city', 'property-type', 'property-status' );
foreach( $taxonomies as $taxonomy ) {
$taxonomy->show_in_rest = true;
$taxonomy->name;
}
}

How can I create a button that both triggers a shortcode and redirects to a specific URL?

I have a wordpress website and i am using a user role plugin.
I need to create a button that triggers to actions:
Fires the shortcode that assigns the user who clicked the button with a specific user role
Redirect the user to a specific URL to view their dashboard
How can I add the shortcode [groups_join group="Buyers"] and the URL www.xyz.com/buyerdashoboard behind a button?
First, you want to create an ajax function to trigger your shortcode like:
function fire_shortcode(){
do_shortcode( '[groups_join group="Buyers"]' );
die();
}
add_action( 'wp_ajax_fire_shortcode', 'fire_shortcode' );
add_action( 'wp_ajax_nopriv_fire_shortcode', 'fire_shortcode' );
Then in your javascript file add something like:
jQuery('.button-selector').click(function(){
var data = {
action: 'fire_shortcode',
arg1: 'value',
arg2: 'value'
};
var ajaxurl = 'http://yourwebsite.com/wp-admin/admin-ajax.php';
$.post(ajaxurl, data, function(response) {
console.log('Shortcode fired. Redirecting now...');
window.location = "http://redirectwebsite.com/";
});
});

Output post for second wordpress editor

I recently added a new editor to all my pages and posts admin area with the Wordpress 3.3 new feature to do this
add_action( 'edit_page_form', 'my_second_editor' );
function my_second_editor() {
// get and set $content somehow...
wp_editor( $content, 'mysecondeditor' );
}
My question is how do I output what I enter in this second editor on my website/page? Do I need to make a custom loop? The codex is not very helpful unfortunately.
Thanks
You'll need get_post_meta(), use it like:
echo get_post_meta(get_the_id(), 'mysecondeditor');
Read more: http://codex.wordpress.org/Function_Reference/get_post_meta
To save the data entered in your second editor you'll need this code in your functions.php file:
add_action( 'save_post', 'save_post', 10, 2 );
function save_post( $post_id, $post ) {
if ( !current_user_can( 'edit_post', $post_id ) )
return $post_id;
update_post_meta( $post_id, 'mysecondeditor', stripslashes( $_POST['mysecondeditor'] ) );
}
So after that he's the full code for your second editor:
wp_editor( get_post_meta(get_the_id(), 'mysecondeditor', true), 'mysecondeditor' );
The true above makes sure only a single variable is returned and not an array so you can use it right away.
user2019515I's (Apr 18 '13 at 12:06) answer works for me, I can add text and gallery, but when I display that with this code:
<?php echo get_post_meta(get_the_ID(),'mysecondeditor')['0']; ?>
I got gallery code instead of the images, so:
mytext [gallery ids="102,62"]
How could I display the text (mytext) and the images too?