I have a function that is currently operating correctly.
It is currently located in the 'My Account' section and is working as it is supposed to. Changing user meta info in the back-end fro a drop-down menu located on the front-end.
The issue is I would like this drop-down to be called from anywhere I like, functioning correctly. For example, it is working in 'My Account' but I also want this on the homepage and a couple of other places.
Code that is working in 'My Account' :
// -------------------------------------------
// CHANGE DEFAULT CITY ON FRONTEND DASHBOARD
function location_select_account() { ?>
<script>
jQuery(document).ready( function(){
jQuery('#location_select').select2();
});
</script> <?php
global $current_user;
// Get New User Meta
if(isset($_POST['location_select'])) {
$locationposter = $_POST['location_select'];
// Update/Create User Meta
update_user_meta( $current_user->ID, 'location_select', $locationposter);
}
else {
// Get User Meta
$locationposter = get_user_meta( $current_user->ID, 'location_select', true);
}
?>
<form method="POST">
<?php
//get dropdown saved value
$selectedinfo = get_the_author_meta( 'location_select', $current_user->ID );
$selected = isset( $selectedinfo['location_select'] ) ? esc_attr( $selectedinfo['location_select'][0] ) :'';
?>
<select name="location_select" id="location_select" style="width:13em;" onchange="this.form.submit()">
<option value="all_of_canada" <?php echo ($selectedinfo == "all_of_canada")? 'selected="all_of_canada"' : '' ?>>All of Canada</option>
<option value="abbotsford" <?php echo ($selectedinfo == "abbotsford")? 'selected="abbotsford"' : '' ?>>Abbotsford</option>
<option value="barrie" <?php echo ($selectedinfo == "barrie")? 'selected="barrie"' : '' ?>>Barrie</option>
<option value="brantford" <?php echo ($selectedinfo == "brantford")? 'selected="brantford"' : '' ?>>Brantford</option>
</select>
</form>
<?php
}
// Add Hook
add_action( 'location_select_account_init' , 'location_select_account' );
add_action( 'woocommerce_account_content' , 'location_select_account_init' , 5 );
function location_select_account_init() {
do_action('location_select_account_init');
}