Adding icon to each category in wordpress - php

I tried to add icons to left of the category names with the following codes;
/** Add Custom Field To Category Form */
add_action( 'category_add_form_fields', 'category_form_custom_field_add', 10 );
add_action( 'category_edit_form_fields', 'category_form_custom_field_edit', 10, 2 );
function category_form_custom_field_add( $taxonomy ) {
?>
<div class="form-field">
<label for="category_custom_order">Simge Kodu</label>
<input name="category_custom_order" id="category_custom_order" type="text" value="" size="40" aria-required="true" />
<p class="description">Dökümanda bulunan simgelerden birini seçip kodunu buraya girin.</p>
</div>
<?php
}
function category_form_custom_field_edit( $tag, $taxonomy ) {
$option_name = 'category_custom_order_' . $tag->term_id;
$category_custom_order = get_option( $option_name );
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="category_custom_order">Simge Kodu</label></th>
<td>
<input type="text" name="category_custom_order" id="category_custom_order" value="<?php echo esc_attr( $category_custom_order ) ? esc_attr( $category_custom_order ) : ''; ?>" size="40" aria-required="true" />
<p class="description">Dökümanda bulunan simgelerden birini seçip kodunu buraya girin.</p>
</td>
</tr>
<?php
}
/** Save Custom Field Of Category Form */
add_action( 'created_category', 'category_form_custom_field_save', 10, 2 );
add_action( 'edited_category', 'category_form_custom_field_save', 10, 2 );
function category_form_custom_field_save( $term_id, $tt_id ) {
if ( isset( $_POST['category_custom_order'] ) ) {
$option_name = 'category_custom_order_' . $term_id;
update_option( $option_name, $_POST['category_custom_order'] );
}
}
?>
After that, i added the following codes to the theme. I want to use get_option function to get icons which belongs to each categories. However, i cannot get category id, so it cannot work. How can i get category id's? I mean, in the code, get_option('category_custom_order_{menu ID}' ), {menu ID} should return the each category id. Can you please help, thanks.
<?php
$defaults = array(
'theme_location' => 'menu',
'menu' => 'Üst Menü',
'container' => '',
'container_class' => '',
'container_id' => '',
'menu_class' => 'menu',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '<i class="fa '.get_option('category_custom_order_{menu ID}' ).' fa-fw"></i> ',
'link_after' => '',
'items_wrap' => '%3$s',
'depth' => 0,
'walker' => ''
);
wp_nav_menu( $defaults );
?>

Related

Custom fields code and displaying in the Additional Information table in Woocommerce

I have adapted the code in the Display selected variation custom fields value in WooCommerce product additional information tab post in order to be able to have custom dimension fields in both the product shipping tab area and the product variations tab area of product info. Here is my adapted code:
// Add custom fields to product shipping tab
add_action( 'woocommerce_product_options_dimensions', 'add_product_options_other_dimensions');
function add_product_options_other_dimensions(){
global $product_object;
$product_id = method_exists( $product_object, 'get_id' ) ? $product_object->get_id() : $product_object->id;
echo '</div><div class="options_group">'; // New option group
woocommerce_wp_text_input( array(
'id' => 'real_length',
'class' => 'short',
'label' => __( 'Actual Length', 'woocommerce' ),
'placeholder' => 'L',
'desc_tip' => 'true',
'description' => __( 'Product actual length (in inches).', 'woocommerce' ),
) );
woocommerce_wp_text_input( array(
'id' => 'real_width',
'class' => 'short',
'label' => __( 'Actual Width', 'woocommerce' ),
'placeholder' => 'W',
'desc_tip' => 'true',
'description' => __( 'Product actual width (in inches).', 'woocommerce' ),
) );
woocommerce_wp_text_input( array(
'id' => 'real_height',
'class' => 'short',
'label' => __( 'Actual Height', 'woocommerce' ),
'placeholder' => 'H',
'desc_tip' => 'true',
'description' => __( 'Product actual height (in inches).', 'woocommerce' ),
) );
}
// Save the custom fields values as meta data
add_action( 'woocommerce_process_product_meta', 'save_product_options_other_dimensions' );
function save_product_options_other_dimensions( $post_id ){
if( isset( $_POST['real_length'] ) )
update_post_meta( $post_id, 'real_length', esc_attr( $_POST['real_length'] ) );
if( isset( $_POST['real_width'] ) )
update_post_meta( $post_id, 'real_width', esc_attr( $_POST['real_width'] ) );
if( isset( $_POST['real_height'] ) )
update_post_meta( $post_id, 'real_height', esc_attr( $_POST['real_height'] ) );
}
// Add custom fields to product variation settings
add_action( 'woocommerce_product_after_variable_attributes','add_variation_options_other_dimensions', 10, 3 );
function add_variation_options_other_dimensions( $loop, $variation_data, $variation ){
$variation_real_length = get_post_meta($variation->ID,"real_length", true );
if( ! $variation_real_length ) $variation_real_length = "";
$variation_real_width = get_post_meta($variation->ID,"real_width", true );
if( ! $variation_real_width ) $variation_real_width = "";
$variation_real_height = get_post_meta($variation->ID,"real_height", true );
if( ! $variation_real_height ) $variation_real_height = "";
echo '<p class="form-field dimensions_field">';
woocommerce_wp_text_input( array(
'id' => 'real_length' . '_' . $loop,
'class' => 'short',
'label' => __( 'Actual Length', 'woocommerce' ),
'placeholder' => 'L',
'desc_tip' => 'true',
'description' => __( 'Product actual length (in inches).', 'woocommerce' ),
'value' => $variation_real_length
) );
woocommerce_wp_text_input( array(
'id' => 'real_width' . '_' . $loop,
'class' => 'short',
'label' => __( 'Actual Width', 'woocommerce' ),
'placeholder' => 'W',
'desc_tip' => 'true',
'description' => __( 'Product actual width (in inches).', 'woocommerce' ),
'value' => $variation_real_width
) );
woocommerce_wp_text_input( array(
'id' => '_circuit' . '_' . $loop,
'class' => 'short',
'label' => __( 'Actual Height', 'woocommerce' ),
'placeholder' => 'H',
'desc_tip' => 'true',
'description' => __( 'Product actual height (in inches).', 'woocommerce' ),
'value' => $variation_real_height
) );
echo '</p>';
}
// Save product variation custom fields values
add_action( 'woocommerce_save_product_variation','save_variation_options_other_dimensions', 10 ,2 );
function save_variation_options_other_dimensions( $variation_id, $loop ){
$the_actual_lenght = $_POST["actual_length_$loop"];
if( isset($the_actual_lenght) )
update_post_meta( $variation_id, 'the_actual_lenght', esc_attr($the_actual_lenght) );
$the_actual_width = $_POST["actual_thickness_$loop"];
if( isset($the_actual_width) )
update_post_meta( $variation_id, 'the_actual_width', esc_attr($the_actual_width) );
$the_actual_height = $_POST["actual_height_$loop"];
if( isset($the_actual_height) )
update_post_meta( $variation_id, 'the_actual_height', esc_attr($the_actual_height) );
}
I have two problems though. For one, the code seems to be a little flawed in that it does not seem to save the custom length/width/height fields under the product variation area. I have checked and the flaw is in the original post's code too.
Secondly, the answer on that post does not detail how to code this so that the inputted information can show on the front end within the Additional Product Information table of Woocommerce product listings.
My question is: how do I alter this code to get it to save the product variation fields and to get the custom field values to display on the Woocommerce Additional Information Tab?
I have been working on this solution for days now and any help would be greatly appreciated.
I ended up paying someone to solve this for me but I figured that I would post the answer here. If anyone out there is looking for a solution to add a second set of dimensions to simple product shipping area and variations area and then have that display in the additional information tab, then here is the code solution:
/**
* Plugin Name: Custom Product Fields
*/
defined('ABSPATH') || exit;
/**
* Add and show simple and variable products.
* Class NFCPF
* #package NFCPF
*/
if (!class_exists('NFCPF')) {
class NFCPF
{
/**
* Hook for call.
* NFCPF constructor.
*/
public function __construct()
{
// Add simple product fields
add_action('woocommerce_product_options_shipping', [$this, 'np_show_product_fields'], 101, 3);
add_action('woocommerce_process_product_meta', [$this, 'np_save_product_fields'], 101, 1);
// Add variable product fields.
add_action('woocommerce_product_after_variable_attributes', [$this, 'np_show_product_fields'], 101, 3);
add_action('woocommerce_save_product_variation', [$this, 'np_save_product_fields'], 101, 1);
}
/**
* Show product fields in variation and simple product.
* #param array $loop
* #param array $variation_data
* #param array $variation
*/
public function np_show_product_fields($loop, $variation_data = [], $variation = [])
{
$postId = (isset($variation->ID)) ? $variation->ID : get_the_ID();
$this->np_custom_product_fields($postId);
}
/**
* Save the simple product fields.
* #param int $postId
*/
public function np_save_product_fields($postId)
{
if (isset($postId) && $postId > 0) {
$np_product_fields = $this->np_product_fields_arr();
foreach ($np_product_fields as $key => $custom_field) {
$custom_field = (isset($custom_field['id'])) ? $custom_field['id'] : '';
$np_updated_product = (isset($_POST[$custom_field][$postId])) ? $_POST[$custom_field][$postId] : '';
update_post_meta($postId, $custom_field, $np_updated_product);
}
}
}
/**
* Product Fields Array
* #return array
*/
public function np_product_fields_arr()
{
$np_product_fields = [
[
'type' => 'input_field',
'id' => 'real_length',
'class' => 'short',
'label' => 'Actual Length',
'placeholder' => 'L',
'description' => __('Product actual length (in inches).', 'woocommerce')
],
[
'type' => 'input_field',
'id' => 'real_width',
'class' => 'short',
'label' => 'Actual Width',
'placeholder' => 'W',
'description' => __('Product actual width (in inches).', 'woocommerce')
],
[
'type' => 'input_field',
'id' => 'real_height',
'class' => 'short',
'label' => 'Actual Height',
'placeholder' => 'H',
'description' => __('Product actual height (in inches).', 'woocommerce')
]
];
return $np_product_fields;
}
/**
* Show Product Fields
* #param int $postId
*/
public function np_custom_product_fields($postId)
{
$np_product_fields = $this->np_product_fields_arr();
foreach ($np_product_fields as $key => $custom_field) {
$np_field_type = (isset($custom_field['type'])) ? $custom_field['type'] : '';
$np_action_function_name = 'np_product_' . $np_field_type;
if (method_exists($this, $np_action_function_name)) {
$this->$np_action_function_name($custom_field, $postId);
}
}
}
/**
* Dynamic input field show on product detail page
* #param array $custom_field
* #param int $postId
*/
public function np_product_input_field($custom_field, $postId)
{
$custom_input_field = [
'id' => $custom_field['id'] . '[' . $postId . ']',
'label' => $custom_field['label'],
'class' => $custom_field['class'],
'placeholder' => $custom_field['placeholder'],
'value' => get_post_meta($postId, $custom_field['id'], true)
];
if (isset($custom_field['description'])) {
$custom_input_field['desc_tip'] = true;
$custom_input_field['description'] = $custom_field['description'];
}
woocommerce_wp_text_input($custom_input_field);
}
}
new NFCPF();
}
/**
* Add data to json encoded variation form.
*
* #param array $data - this is the variation's json data
* #param object $product
* #param object $variation
* #return array
*/
function kia_available_variation($data, $product, $variation)
{
$kia_data['real_length'] = $variation->get_meta('real_length', true);
$kia_data['real_width'] = $variation->get_meta('real_width', true);
$kia_data['real_height'] = $variation->get_meta('real_height', true);
return array_merge($data, $kia_data);
}
add_filter('woocommerce_available_variation', 'kia_available_variation', 10, 3);
/**
* Add scripts to variable products.
*/
function kia_on_found_template_for_variable_add_to_cart()
{
add_action('wp_print_footer_scripts', 'kia_variable_footer_scripts', 99);
}
add_action('woocommerce_variable_add_to_cart', 'kia_on_found_template_for_variable_add_to_cart', 30);
function kia_variable_footer_scripts()
{ ?>
<script type="text/template" id="tmpl-variation-template-extra-data">
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--attribute_real_length extra-data">
<th class="woocommerce-product-attributes-item__label"><?php esc_html_e('Length (in)', 'my-text-domain'); ?></th>
<td class="woocommerce-product-attributes-item__value">{{{ data.variation.real_length }}}</td>
</tr>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--attribute_real_width extra-data">
<th class="woocommerce-product-attributes-item__label"><?php esc_html_e('Width (in)', 'my-text-domain'); ?></th>
<td class="woocommerce-product-attributes-item__value">{{{ data.variation.real_width }}}</td>
</tr>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--attribute_real_height extra-data">
<th class="woocommerce-product-attributes-item__label"><?php esc_html_e('Height (in)', 'my-text-domain'); ?></th>
<td class="woocommerce-product-attributes-item__value">{{{ data.variation.real_height }}}</td>
</tr>
</script>
<script type="text/javascript">
jQuery(document).ready(function ($) {
$('form.cart')
.on('found_variation', function (event, variation) {
template = wp.template('variation-template-extra-data');
$template_html = template({
variation: variation
});
// Remove any existing rows.
$('#tab-additional_information').find('.woocommerce-product-attributes tr.extra-data').remove();
// Add new rows.
$('#tab-additional_information').find('.woocommerce-product-attributes tbody').append($template_html);
})
.on('reset_data', function (event, variation) {
$('#tab-additional_information').find('.woocommerce-product-attributes tr.extra-data').remove();
});
});
</script>
<?php
}
// Show custom field to simple product
function np_woocommerce_product_additional_information($product)
{
if ($product->is_type('simple')) {
$product_id = $product->get_id();
$real_length = get_post_meta($product_id, 'real_length', true);
$real_width = get_post_meta($product_id, 'real_width', true);
$real_height = get_post_meta($product_id, 'real_height', true);
ob_start();
?>
<table class="woocommerce-product-attributes shop_attributes">
<tbody>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--attribute_real_length extra-data">
<th class="woocommerce-product-attributes-item__label"><?php esc_html_e('Length (in)', 'np-domain'); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo $real_length; ?></td>
</tr>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--attribute_real_width extra-data">
<th class="woocommerce-product-attributes-item__label"><?php esc_html_e('Width (in)', 'np-domain'); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo $real_width; ?></td>
</tr>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--attribute_real_height extra-data">
<th class="woocommerce-product-attributes-item__label"><?php esc_html_e('Height (in)', 'np-domain'); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo $real_height; ?></td>
</tr>
</tbody>
</table>
<?php
echo ob_get_clean();
}
}
add_action('woocommerce_product_additional_information', 'np_woocommerce_product_additional_information', 99, 1);
This solution is particularly useful for anyone looking to have both "shipping dimensions" (which are provided by default by Woocommerce) and "product dimensions" which would be displayed to the customer.
In the meantime you have answered your question yourself (+1) but for the first part of your question:
"For one, the code seems to be a little flawed in that it does not seem to save the custom length/width/height fields under the product variation area. I have checked and the flaw is in the original post's code too."
This may be useful for you or other users
To add custom fields to the product shipping tab you can use:
// Add custom fields to product shipping tab
function action_woocommerce_product_options_dimensions() {
// New option group
echo '</div><div class="options_group">';
// Fields
woocommerce_wp_text_input( array(
'id' => 'real_length',
'class' => 'short',
'label' => __( 'Actual Length', 'woocommerce' ),
'placeholder' => 'L',
'desc_tip' => 'true',
'description' => __( 'Product actual length (in inches).', 'woocommerce' ),
) );
woocommerce_wp_text_input( array(
'id' => 'real_width',
'class' => 'short',
'label' => __( 'Actual Width', 'woocommerce' ),
'placeholder' => 'W',
'desc_tip' => 'true',
'description' => __( 'Product actual width (in inches).', 'woocommerce' ),
) );
woocommerce_wp_text_input( array(
'id' => 'real_height',
'class' => 'short',
'label' => __( 'Actual Height', 'woocommerce' ),
'placeholder' => 'H',
'desc_tip' => 'true',
'description' => __( 'Product actual height (in inches).', 'woocommerce' ),
) );
}
add_action( 'woocommerce_product_options_dimensions', 'action_woocommerce_product_options_dimensions' );
However, if you want to display it on 1 line then use this instead of the above code!
function action_woocommerce_product_options_dimensions() {
global $product_object;
?>
<p class="form-field actual_dimensions_field dimensions_field">
<?php /* translators: WooCommerce dimension unit*/ ?>
<label for="product_real_length"><?php printf( __( 'Actual dimensions (%s)', 'woocommerce' ), get_option( 'woocommerce_dimension_unit' ) ); ?></label>
<span class="wrap">
<input id="product_real_length" placeholder="<?php esc_attr_e( 'Actual length', 'woocommerce' ); ?>" class="input-text wc_input_decimal" size="6" type="text" name="product_real_length" value="<?php echo esc_attr( wc_format_localized_decimal( $product_object->get_meta( 'product_real_length' ) ) ); ?>" />
<input id="product_real_width" placeholder="<?php esc_attr_e( 'Actual width', 'woocommerce' ); ?>" class="input-text wc_input_decimal" size="6" type="text" name="product_real_width" value="<?php echo esc_attr( wc_format_localized_decimal( $product_object->get_meta( 'product_real_width' ) ) ); ?>" />
<input id="product_real_height" placeholder="<?php esc_attr_e( 'Actual height', 'woocommerce' ); ?>" class="input-text wc_input_decimal last" size="6" type="text" name="product_real_height" value="<?php echo esc_attr( wc_format_localized_decimal( $product_object->get_meta( 'product_real_height' ) ) ); ?>" />
</span>
<?php echo wc_help_tip( __( 'Your text', 'woocommerce' ) ); ?>
</p>
<?php
}
add_action( 'woocommerce_product_options_dimensions', 'action_woocommerce_product_options_dimensions' );
To save fields you can use the woocommerce_admin_process_product_object hook, opposite the outdated woocommerce_process_product_meta hook:
// Save the custom fields values as meta data
function action_woocommerce_admin_process_product_object( $product ) {
// When isset, save
if ( isset( $_POST['product_real_length'] ) ) {
$product->update_meta_data( 'product_real_length', esc_attr( $_POST['product_real_length'] ) );
}
if ( isset( $_POST['product_real_width'] ) ) {
$product->update_meta_data( 'product_real_width', esc_attr( $_POST['product_real_width'] ) );
}
if ( isset( $_POST['product_real_height'] ) ) {
$product->update_meta_data( 'product_real_height', esc_attr( $_POST['product_real_height'] ) );
}
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );
So far adding and saving the fields to the product shipping tab.
To add and save the fields to the variations tab area, you can use the answer below. But, here are some remarks:
The woocommerce_product_after_variable_attributes hook is replaced by woocommerce_variation_options_dimensions as it is better positioned for your question
woocommerce_admin_process_variation_object replaces the outdated woocommerce_save_product_variation
Optional: to align the fields to the left change form-row-last to form-row-first
// Add fields
function action_woocommerce_variation_options_dimensions( $loop, $variation_data, $variation ) {
global $post;
// Parent product
$product_object = wc_get_product( $post->ID );
// Get meta
$parent_length = wc_format_localized_decimal( $product_object->get_meta( 'product_real_length' ) );
$parent_width = wc_format_localized_decimal( $product_object->get_meta( 'product_real_width' ) );
$parent_height = wc_format_localized_decimal( $product_object->get_meta( 'product_real_height' ) );
$real_length = get_post_meta( $variation->ID, 'variable_real_length', true );
$real_width = get_post_meta( $variation->ID, 'variable_real_width', true );
$real_height = get_post_meta( $variation->ID, 'variable_real_height', true );
?>
<p class="form-field form-row actual_dimensions_field dimensions_field hide_if_variation_virtual form-row-last">
<label for="product_real_length">
<?php
printf(
/* translators: %s: dimension unit */
esc_html__( 'Your text (%s)', 'woocommerce' ),
esc_html( get_option( 'woocommerce_dimension_unit' ) )
);
?>
</label>
<?php echo wc_help_tip( __( 'Your text', 'woocommerce' ) ); ?>
<span class="wrap">
<input id="product_real_length" placeholder="<?php echo $parent_length ? esc_attr( $parent_length ) : esc_attr__( 'Actual length', 'woocommerce' ); ?>" class="input-text wc_input_decimal" size="6" type="text" name="variable_real_length[<?php echo esc_attr( $loop ); ?>]" value="<?php echo esc_attr( wc_format_localized_decimal( $real_length ) ); ?>" />
<input id="product_real_width" placeholder="<?php echo $parent_width ? esc_attr( $parent_width ) : esc_attr__( 'Actual width', 'woocommerce' ); ?>" class="input-text wc_input_decimal" size="6" type="text" name="variable_real_width[<?php echo esc_attr( $loop ); ?>]" value="<?php echo esc_attr( wc_format_localized_decimal( $real_width ) ); ?>" />
<input id="product_real_height" placeholder="<?php echo $parent_height ? esc_attr( $parent_height ) : esc_attr__( 'Actual height', 'woocommerce' ); ?>" class="input-text wc_input_decimal last" size="6" type="text" name="variable_real_height[<?php echo esc_attr( $loop ); ?>]" value="<?php echo esc_attr( wc_format_localized_decimal( $real_height ) ); ?>" />
</span>
</p>
<?php
}
add_action( 'woocommerce_variation_options_dimensions', 'action_woocommerce_variation_options_dimensions', 10, 3 );
// Save
function action_woocommerce_admin_process_variation_object( $variation, $i ) {
// When isset, save
if ( isset( $_POST['variable_real_length'][$i] ) ) {
$variation->update_meta_data( 'variable_real_length', esc_attr( $_POST['variable_real_length'][$i] ) );
}
if ( isset( $_POST['variable_real_width'][$i] ) ) {
$variation->update_meta_data( 'variable_real_width', esc_attr( $_POST['variable_real_width'][$i] ) );
}
if ( isset( $_POST['variable_real_height'][$i] ) ) {
$variation->update_meta_data( 'variable_real_height', esc_attr( $_POST['variable_real_height'][$i] ) );
}
}
add_action( 'woocommerce_admin_process_variation_object', 'action_woocommerce_admin_process_variation_object', 10, 2 );
Result:

Wordpress settings menu not displaying correctly

I've create a settings menu with tabs and some content. I get no PHP errors but when I load the page my settings are looking like this:
So as you can see my content is outside of the tab in the left corner. I'm absolutely not sure whats wrong here. This is my code:
Here I register the new menu page:
add_action( 'admin_menu', 'register_settings_submenu' );
function register_settings_submenu() {
add_submenu_page( 'options-general.php', 'Additional Settings', 'Additional Settings', 'manage_options', 'settings-submenu', 'settings_page' );
}
This is the regarding function for the add_action above:
function settings_page() {
global $settings_active_tab;
$settings_active_tab = isset( $_GET['tab'] ) ? $_GET['tab'] : 'general'; ?>
<h2 class="nav-tab-wrapper">
<?php
do_action( 'payments_tab' )
?>
</h2>
<?php
do_action( 'payments_content' );
}
This is the function for the tab:
add_action( 'payments_tab', 'payments_tab', 1 );
function payments_tab() {
global $settings_active_tab; ?>
<a class="nav-tab <?php echo $settings_active_tab === 'payments' || '' ? 'nav-tab-active' : ''; ?>"
href="<?php echo admin_url( 'admin.php?page=settings-submenu&tab=payments' ); ?>"><?php _e( 'Zahlungen ', 'woocommerce' ); ?> </a>
<?php
}
This is the function for the content:
add_action( 'payments_content', 'payments_content' );
function payments_content() {
global $settings_active_tab;
if ( '' || 'payments' !== $settings_active_tab ) {
return;
} ?>
<div class="wrap">
<h2>Zahlungen</h2>
<form method="post" action="options.php">
<table class="form-table">
<tbody>
<?php
settings_fields( "payments" );
do_settings_sections( "payment-options" );
?>
</tbody>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php }
This function gets the content for this tab and init it:
add_action( "admin_init", "display_settings_pages_content" );
function display_settings_pages_content() {
payment_settings_element();
}
This is the function payment_settings_element:
function payment_settings_element() {
$settings = array(
'field1' => array(
'name' => __( 'Field 1', 'settings' ),
'id' => 'wc_tab_field1',
'type' => 'text',
'desc_tip' => __( 'Im a short desc', 'settings' ),
'desc' => __( '%', 'settings' )
),
'field2' => array(
'name' => __( 'Field 2', 'settings' ),
'id' => 'wc_tab_field2',
'type' => 'text',
'desc_tip' => __( 'Another short desc', 'settings' ),
'desc' => __( '$', 'settings' )
)
);
?>
<?php
foreach ( $settings as $key => $setting ) {
add_settings_field( $key, $setting['name'], createInput( $setting['name'], $setting['id'], $setting['type'], $setting['desc'] ), 'payment-options', 'payments' );
register_setting( 'payments', $key );
} ?>
<?php }
And finally the functions which creates a new settings element:
function createInput( $name, $id, $type, $desc ) {
?>
<tr valign="top">
<th scope="row" class="titledesc">
<label for="<?php echo $id; ?>"><?php echo $name; ?></label>
</th>
<td class="forminp forminp-text">
<input type="<?php echo $type; ?>" name="<?php echo $id; ?>" id="<?php echo $id; ?>"
value="<?php echo get_option( $id ); ?>">
<span class="description"><?php echo $desc; ?></span>
</td>
</tr>
<?php
}
I've absolutely no clue why the inputs are outside of the tab content which has just the table structure but no entries. What do I wrong?
You used the wrong hook here:
add_action( "admin_init", "display_settings_pages_content" );
Try this:
add_action( "payments_content", "display_settings_pages_content" );

WP Job manager Location dropdown

Im using the WP Job Manager plug on my website. On the home page im using the Hero search filter that has the location search input, that is a text input at the moment but I would like to change it to a dropdown with set options. I've been able to easily change the location text input to a dropdown with the chosen options.
I hit search, nothing changes in the job listings below.
I've tried to change the way the input is handled in the source files but with no luck. When i output the result of the 'Location' input on the job listing page but it keeps coming back as an empty field.
I have no idea what els to try to accomplish the desired result any more so any help or input would be appreciated.
The code below is how my part-job-filters.php code.
<?php
if ( ! class_exists( 'WP_Job_Manager' ) ) {
return;
}
$keywords = '';
$location = array();
$selected_category = array();
if ( ! empty( $_GET['search_keywords'] ) ) {
$keywords = sanitize_text_field( $_GET['search_keywords'] );
}
// Old Location input field
// if ( ! empty( $_GET['search_location'] ) ) {
// $location = sanitize_text_field( $_GET['search_location'] );
// }
if ( ! empty( $_GET['search_location'] ) ) {
$location = absint( $_GET['search_location'] );
}
if ( ! empty( $_GET['search_category'] ) ) {
if ( is_array( $_GET['search_category'] ) ) {
$selected_category = absint( reset( $_GET['search_category'] ) );
} else {
$selected_category = absint( $_GET['search_category'] );
}
$_GET['search_category'] = $selected_category;
}
if ( is_tax( 'job_listing_category' ) ) {
$term = get_queried_object();
$_GET['search_category'] = $selected_category = array( $term->term_id );
}
?>
<div class="form-filter">
<div class="form-filter-header">
×<span class="sr-only"> <?php esc_html_e( 'Dismiss filters', 'specialty' ); ?></span>
</div>
<?php
$has_categories = true;
$count = wp_count_terms( 'job_listing_category', array(
'hide_empty' => 1, // Hide empty, as they are not displayed by the dropdown anyway.
) );
if ( is_wp_error( $count ) || 0 === intval( $count ) || is_tax( 'job_listing_category' ) ) {
$has_categories = false;
}
$col_classes = array(
'keywords' => 'col-lg-3 col-xs-12',
'location' => 'col-lg-3 col-xs-12',
'category' => 'col-lg-3 col-xs-12',
'button' => 'col-lg-3 col-xs-12',
);
if ( ! get_option( 'job_manager_enable_categories' ) || ! $has_categories ) {
$col_classes = array(
'keywords' => 'col-lg-4 col-xs-12',
'location' => 'col-lg-4 col-xs-12',
'category' => '',
'button' => 'col-lg-3 push-lg-1 col-xs-12',
);
}
?>
<div class="container">
<div class="row">
<div class="<?php echo esc_attr( $col_classes['keywords'] ); ?>">
<label for="job-keywords" class="sr-only"><?php esc_html_e( 'Job Keywords', 'specialty' ); ?></label>
<input type="text" id="job-keywords" name="search_keywords" placeholder="<?php esc_attr_e( 'Keywords', 'specialty' ); ?>" value="<?php echo esc_attr( $keywords ); ?>">
</div>
<div class="<?php echo esc_attr( $col_classes['location'] ); ?>">
<label for="job-location" class="sr-only"><?php esc_html_e( 'Job Location', 'specialty' ); ?></label>
<div class="ci-select">
<select name="search_location" id="job-location" data-no_results_text="No results match" data-multiple_text="Select Some Options">
<option value="0">Select a location</option>
<option value="Eastern Cape">Eastern Cape</option>
<option value="Free State">Free State</option>
<option value="Gauteng">Gauteng</option>
<option value="kwazulu natal">kwazulu natal</option>
<option value="Limpopo">Limpopo</option>
<option value="Mpumalanga">Mpumalanga</option>
<option value="North West">North West</option>
<option value="Northern Cape">Northern Cape</option>
<option value="Western Cape">Western Cape</option>
<option value="Africa">Africa</option>
<option value="International">International</option>
<option value="All locations">All locations</option>
</select>
</div>
</div>
<?php if ( get_option( 'job_manager_enable_categories' ) && $has_categories ) : ?>
<div class="<?php echo esc_attr( $col_classes['category'] ); ?>">
<label for="job-category" class="sr-only"><?php esc_html_e( 'Job Category', 'specialty' ); ?></label>
<div class="ci-select">
<?php
$multiple = get_option( 'job_manager_enable_default_category_multiselect' );
job_manager_dropdown_categories( array(
'taxonomy' => 'job_listing_category',
'hierarchical' => 1,
'show_option_all' => $multiple ? false : esc_html__( 'Any category', 'specialty' ),
'name' => 'search_category',
'orderby' => 'name',
'selected' => $selected_category,
'multiple' => $multiple,
) );
?>
</div>
</div>
<?php endif; ?>
<div class="<?php echo esc_attr( $col_classes['button'] ); ?>">
<button class="btn btn-block btn-jobs-filter" type="submit"><?php esc_html_e( 'Search', 'specialty' ); ?></button>
</div>
</div>
</div>
</div>
I might be approaching this the wrong way, if so please could someone advise what the right way is of approaching this.
Replace id in select element, id="job-location" to id="search_location".

conflict in taxonomy image fields

i use this code to create custom taxonomy with image.
when i duplicate code and change "writer" to any other as "car".
it work but with error in image field.
so i can not now choose an image for both taxonomies.
add_action( 'init', 'create_writers_nonhierarchical_taxonomy', 0 );
function create_writers_nonhierarchical_taxonomy() {
// Labels part for the GUI
$labels = array(
'name' => _x( 'Writers', 'taxonomy general name' ),
'singular_name' => _x( 'Writer', 'taxonomy singular name' ),
'search_items' => __( 'Search Writers' ),
'popular_items' => __( 'Popular Writers' ),
'all_items' => __( 'All Writers' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Writer' ),
'update_item' => __( 'Update Writer' ),
'add_new_item' => __( 'Add New Writer' ),
'new_item_name' => __( 'New Writer Name' ),
'separate_items_with_commas' => __( 'Separate writers with commas' ),
'add_or_remove_items' => __( 'Add or remove writers' ),
'choose_from_most_used' => __( 'Choose from the most used writers' ),
'menu_name' => __( 'Writers' ),
);
// Now register the non-hierarchical taxonomy like tag
register_taxonomy('writers','post',array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'show_in_nav_menus' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'writer', 'with_front' => false),
));
function writer_flush_rewrite() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action('init', 'writer_flush_rewrite');
}
/**
* Plugin class
**/
function load_wp_media_files() {
wp_enqueue_media();
}
add_action( 'admin_enqueue_scripts', 'load_wp_media_files' );
if ( ! class_exists( 'CT_TAX_META' ) ) {
class CT_TAX_META {
public function __construct() {
//
}
/*
* Initialize the class and start calling our hooks and filters
* #since 1.0.0
*/
public function init() {
add_action( 'writers_add_form_fields', array ( $this, 'add_writers_image' ), 10, 2 );
add_action( 'created_writers', array ( $this, 'save_writers_image' ), 10, 2 );
add_action( 'writers_edit_form_fields', array ( $this, 'update_writers_image' ), 10, 2 );
add_action( 'edited_writers', array ( $this, 'updated_writers_image' ), 10, 2 );
add_action( 'admin_footer', array ( $this, 'add_script' ) );
}
/*
* Add a form field in the new writers page
* #since 1.0.0
*/
public function add_writers_image ( $taxonomy ) { ?>
<div class="form-field term-group">
<label for="writers-image-id"><?php _e('Image', 'hero-theme'); ?></label>
<input type="hidden" id="writers-image-id" name="writers-image-id" class="custom_media_url" value="">
<div id="writers-image-wrapper"></div>
<p>
<input type="button" class="button button-secondary ct_tax_media_button" id="ct_tax_media_button" name="ct_tax_media_button" value="<?php _e( 'Add Image', 'hero-theme' ); ?>" />
<input type="button" class="button button-secondary ct_tax_media_remove" id="ct_tax_media_remove" name="ct_tax_media_remove" value="<?php _e( 'Remove Image', 'hero-theme' ); ?>" />
</p>
</div>
<?php
}
/*
* Save the form field
* #since 1.0.0
*/
public function save_writers_image ( $term_id, $tt_id ) {
if( isset( $_POST['writers-image-id'] ) && '' !== $_POST['writers-image-id'] ){
$image = $_POST['writers-image-id'];
add_term_meta( $term_id, 'writers-image-id', $image, true );
}
}
/*
* Edit the form field
* #since 1.0.0
*/
public function update_writers_image ( $term, $taxonomy ) { ?>
<tr class="form-field term-group-wrap">
<th scope="row">
<label for="writers-image-id"><?php _e( 'Image', 'hero-theme' ); ?></label>
</th>
<td>
<?php $image_id = get_term_meta ( $term -> term_id, 'writers-image-id', true ); ?>
<input type="hidden" id="writers-image-id" name="writers-image-id" value="<?php echo $image_id; ?>">
<div id="writers-image-wrapper">
<?php if ( $image_id ) { ?>
<?php echo wp_get_attachment_image ( $image_id, 'slider-small' ); ?>
<?php } ?>
</div>
<p>
<input type="button" class="button button-secondary ct_tax_media_button" id="ct_tax_media_button" name="ct_tax_media_button" value="<?php _e( 'Add Image', 'hero-theme' ); ?>" />
<input type="button" class="button button-secondary ct_tax_media_remove" id="ct_tax_media_remove" name="ct_tax_media_remove" value="<?php _e( 'Remove Image', 'hero-theme' ); ?>" />
</p>
</td>
</tr>
<?php
}
/*
* Update the form field value
* #since 1.0.0
*/
public function updated_writers_image ( $term_id, $tt_id ) {
if( isset( $_POST['writers-image-id'] ) && '' !== $_POST['writers-image-id'] ){
$image = $_POST['writers-image-id'];
update_term_meta ( $term_id, 'writers-image-id', $image );
} else {
update_term_meta ( $term_id, 'writers-image-id', '' );
}
}
/*
* Add script
* #since 1.0.0
*/
public function add_script() { ?>
<script>
jQuery(document).ready( function($) {
function ct_media_upload(button_class) {
var _custom_media = true,
_orig_send_attachment = wp.media.editor.send.attachment;
$('body').on('click', button_class, function(e) {
var button_id = '#'+$(this).attr('id');
var send_attachment_bkp = wp.media.editor.send.attachment;
var button = $(button_id);
_custom_media = true;
wp.media.editor.send.attachment = function(props, attachment){
if ( _custom_media ) {
$('#writers-image-id').val(attachment.id);
$('#writers-image-wrapper').html('<img class="custom_media_image" src="" style="margin:0;padding:0;max-height:100px;float:none;" />');
$('#writers-image-wrapper .custom_media_image').attr('src',attachment.sizes.thumbnail.url).css('display','block');
var src = attachment.url;
if (attachment.sizes.thumbnail) {
src = attachment.sizes.thumbnail.url;
}
$('#writers-image-wrapper .custom_media_image').attr('src',src).css('display','block');
} else {
return _orig_send_attachment.apply( button_id, [props, attachment] );
}
}
wp.media.editor.open(button);
return false;
});
}
ct_media_upload('.ct_tax_media_button.button');
$('body').on('click','.ct_tax_media_remove',function(){
$('#writers-image-id').val('');
$('#writers-image-wrapper').html('<img class="custom_media_image" src="" style="margin:0;padding:0;max-height:100px;float:none;" />');
});
// Thanks: http://stackoverflow.com/questions/15281995/wordpress-create-writers-ajax-response
$(document).ajaxComplete(function(event, xhr, settings) {
var queryStringArr = settings.data.split('&');
if( $.inArray('action=add-tag', queryStringArr) !== -1 ){
var xml = xhr.responseXML;
$response = $(xml).find('term_id').text();
if($response!=""){
// Clear the thumb image
$('#writers-image-wrapper').html('');
}
}
});
});
</script>
<?php }
}
$CT_TAX_META = new CT_TAX_META();
$CT_TAX_META -> init();
}

Custom menu Wordpress: add attribute data-letters="link name"

I have a custom menu :
function mfn_wp_nav_menu() {
$args = array(
'container' => 'nav',
'container_id' => 'menu',
'menu_class' => 'menu',
'fallback_cb' => 'mfn_wp_page_menu',
'depth' => 5,
'link_before' => '<span>',
'link_after' => '</span>',
);
}
And the input in HTML is :
<li id="id" class="class">
<a href="#" data-hash="">
<span>Start</span>
</a>
</li>
But I want to add data-letters="Link name", so for example :
<li id="id" class="class">
<a href="#" data-hash="">
<span data-letters="Start">Start</span>
</a>
</li>
Any Idea ?
You can extend the Walker menu class like this:
class MyMenu extends Walker {
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$output .= sprintf( "\n<li><a href='%s'%s><span data-letters='%s'>%s</span></a></li>\n",
$item->url,
( $item->object_id === get_the_ID() ) ? ' class="current"' : '',
$item->title,
$item->title
);
}
}
And apply it to your menu:
wp_nav_menu(array(
'menu' => 2, // menu id
'walker' => new MyMenu()
// your other options
));
See this example from the reference: here

Categories