understand callback function in wordpress - wordpress

I'd like to understand what $args['id'] mean in the add_settings_section() callback function, and how the loop get the id of every section:
here my array to add sections information:
$ap_sections = [
'section-theme-settings' => [
'title' => 'Theme settings',
'desc' => 'General theme settings'
],
'section-styling-settings' => [
'title' => 'Styling settings',
'desc' => 'settings for editing colors, fonts and css.'
],
'section-social-settings' => [
'title' => 'Social settings',
'desc' => 'Edit your social media profiles.'
]
];
here my loop:
foreach ( $ap_sections as $section_id => $section_value ) {
add_settings_section( $section_id, $section_value['title'], 'ap_render_section', 'ap-theme-options' );
}
and here ap_render_section callback function:
function ap_render_section($args) {
global $ap_sections;
echo '<p>' . $ap_sections[ $args['id'] ] ['desc'] . '</p>';
echo '<hr />';
}
my goal is understand not copy and pate code

Each section should have unique ID. And first parameter in add_settings_section function is ID of that added section.
Check the code from WP Codex:
add_settings_section(
'eg_setting_section',
'Example settings section in reading',
'eg_setting_section_callback_function',
'reading'
);
function eg_setting_section_callback_function( $arg ) {
// echo section intro text here
echo '<p>id: ' . $arg['id'] . '</p>'; // id: eg_setting_section
echo '<p>title: ' . $arg['title'] . '</p>'; // title: Example settings section in reading
echo '<p>callback: ' . $arg['callback'] . '</p>'; // callback: eg_setting_section_callback_function
}
As you can see, $arg['id'] here is "eg_setting_section".
So, add_settings_section function passes your added unique slug and title to callback function as array elements.

Related

buddypress pass an argument in a button

I use wordpress with the buddypress plugin.
I am making a plugin in which I have a button.
function bpbc_add_custom_buttons() {
global $bp;
$new_contact_button_args = array(
'id' => 'bpbc_new_contact',
'component' => 'members',
'must_be_logged_in' => true,
'block_self' => true,
'link_href' => esc_url( $bp->loggedin_user->domain . 'contacts/?id=' . $bp->displayed_user->id),
'link_text' => __( 'Add new contact' ),
);
echo bp_get_button( $new_contact_button_args );
}
add_action( 'bp_member_header_actions', 'bpbc_add_custom_buttons' );
This is what this button calls
function contacts_screen() {
add_action( 'bp_template_content', 'contacts_screen_content' );
bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
}
function contacts_screen_content() { }
I can't retrieve the argement id that is in my link
'link_href' => esc_url($bp->loggedin_user->domain. 'contacts/?id='. $bp->displayed_user->id)
Thanks for your help
Try:
function contacts_screen_content() {
if ( isset( $_GET['id'] ) ) {
echo 'id: ' . $_GET['id'];
}
}

Display custom checkout field value on admin order detail section in Woocommerce

Hello I'm trying to display the custom checkout field in the admin order details page. My Custom field is Delivery Option and it allows the user to pick the to pick a value from checkbox. I use the code below following the similar topics about this, but it seems something is wrong with my code.
add_action( 'woocommerce_review_order_after_shipping', 'checkout_shipping_additional_field', 20 );
function checkout_shipping_additional_field()
{
$domain = 'wocommerce';
$default = 'option 1';
echo '<tr class="additional-shipping-fields"><th>' . __('Delivery Time', $domain) . '</th><td>';
// Add a custom checkbox field
woocommerce_form_field( 'custom_radio_field', array(
'type' => 'select',
'class' => array( 'form-row-wide' ),
'options' => array(
'option 1' => __('10:04 : 13:04 ', $domain),
),
'default' => $default,
), $default );
echo '</td></tr>';
}
//update order meta
add_action('woocommerce_checkout_update_order_meta', 'gon_update_order_meta_business_address');
function gon_update_order_meta_business_address( $order_id ) {
if ($_POST['custom_radio_field']) update_post_meta( $order_id, 'Business Address?',
esc_attr($_POST['custom_radio_field']));
}
// Display field value on the admin order edit page
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'custom_checkout_field_display_admin_order_meta', 10, 1 );
function custom_checkout_field_display_admin_order_meta( $order ){
$delivery_time = get_post_meta( $order->get_id(), 'Delivery Time', true );
if( ! empty( $delivery_time ) )
echo '<p><strong>'.__('Delivery Time', 'woocommerce').': </strong> ' . $delivery_time . '</p>';
}
There is some mistakes, so I have revisited your code. I have also replaced some hooks. Try the following:
// HERE set your the options array for your select field.
function delivery_time_options(){
$domain = 'woocommerce';
return array(
'1' => __('10:04 : 13:04 ', $domain),
'2' => __('14:04 : 16:04 ', $domain), // <== Added for testing
);
}
// Display a custom select field after shipping total line
add_action( 'woocommerce_review_order_after_shipping', 'checkout_shipping_additional_field', 20 );
function checkout_shipping_additional_field(){
$domain = 'woocommerce';
echo '<tr class="additional-shipping-fields"><th>' . __('Delivery Time', $domain) . '</th><td>';
// Add a custom select field
woocommerce_form_field( 'delivery_time', array(
'type' => 'select',
'class' => array( 'form-row-wide' ),
'options' => delivery_time_options(),
), '' );
echo '</td></tr>';
}
// Save custom field as order meta data
add_action('woocommerce_checkout_create_order', 'save_custom_field_order_meta', 22, 2 );
function save_custom_field_order_meta( $order, $data ) {
if ( isset($_POST['delivery_time']) ) {
$options = delivery_time_options(); // Get select options array
$option_key = esc_attr($_POST['delivery_time']); // The selected key
$order->update_meta_data( '_delivery_time', $options[$option_key] ); // Save
}
}
// Display a custom field value on the admin order edit page
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'display_custom_meta_data_in_backend_orders', 10, 1 );
function display_custom_meta_data_in_backend_orders( $order ){
$domain = 'woocommerce';
$delivery_time = $order->get_meta('_delivery_time');
if( ! empty( $delivery_time ) )
echo '<p><strong>'.__('Delivery Time', $domain).': </strong> ' . $delivery_time . '</p>';
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Based on #LoicTheAztec answer, if you want multiple fields without re-writing the functions for every field (DRY), you can use this class (by adding it to your functions.php):
/**
* Add a custom field to the woocommerce checkout page
* https://stackoverflow.com/q/52098807/
*/
class WOO_Add_Checkout_Field
{
public function __construct($options)
{
$this->field_name = $options['field_name'];
$this->label = $options['label'];
$this->placeholder = $options['placeholder'];
$this->required = $options['required'];
if ($this->field_name && $this->label && $this->placeholder) {
add_action('woocommerce_after_order_notes', [$this, 'customise_checkout_field']);
add_action('woocommerce_checkout_update_order_meta', [$this, 'custom_checkout_field_update_order_meta'], 10, 1);
add_action('woocommerce_admin_order_data_after_billing_address', [$this, 'display_custom_field_on_order_edit_pages'], 10, 1);
} else {
die("Error in WOO_Add_Checkout_Field \$options: \n\n" . var_dump($options));
}
}
public function customise_checkout_field($checkout)
{
echo '<div id="customise_checkout_field">';
// echo '<h2>' . __('Heading') . '</h2>';
woocommerce_form_field($this->field_name, array(
'type' => 'text',
'class' => array(
'my-field-class form-row-wide'
),
'label' => $this->label,
'placeholder' => $this->placeholder,
'required' => $this->required,
), $checkout->get_value($this->field_name));
echo '</div>';
}
public function custom_checkout_field_update_order_meta($order_id)
{
if (!empty($_POST[$this->field_name]))
update_post_meta($order_id, $this->field_name, $_POST[$this->field_name]);
else
update_post_meta($order_id, $this->field_name, 0);
}
public function display_custom_field_on_order_edit_pages($order)
{
$field = $order->get_meta($this->field_name);
if (!empty($field)) {
echo '<p><strong style="display:block" title="' . $this->placeholder . '">' . $this->label . ': </strong><span>';
echo $field;
echo '</span></p>';
}
}
}
And use it as many times as you'd like:
$my_custom_field_1 = new WOO_Add_Checkout_Field([
'field_name' => 'my_custom_field_1',
'label' => __('My First Field'),
'placeholder' => __('Please write something in field 1...'),
'required' => false,
]);
$my_custom_field_2 = new WOO_Add_Checkout_Field([
'field_name' => 'my_custom_field_2',
'label' => __('My Second Field'),
'placeholder' => __('Please write something in field 2...'),
'required' => false,
]);
$my_custom_field_3 = new WOO_Add_Checkout_Field([
'field_name' => 'my_custom_field_3',
'label' => __('My Third Field'),
'placeholder' => __('Please write something in field 3...'),
'required' => false,
]);
// and so on...
Please note:
The custom field will show up in admin area ONLY if it was not sent empty
You can customize this code how you like

How to properly save a WordPress option containing HTML code?

Here's a screenshot of what the problem looks like:
And the HTML of that part of the WordPress options page looks like this:
So, in the WordPress admin area, I entered a piece of HTML code (to have a clickable link as the output on the frontend).
This was the code I had entered into that text input field on the backend:
<a title="Website Design London" href="../../website-design/">Website Design</a>
And while on the frontend that link is displaying OK, I'm seeing this mess (see screenshot) on the backend.
As far as I can tell the relevant PHP code is this:
$this->text(
'workdone',
esc_html__( 'Work done', 'mytheme' )
);
So, what is the proper way to save an option that contains HTML code?
And how can I fix the mess shown on the screenshot?
I had the same issue, and this code is working for me:
// render service label
public function render_service_label() {
$value = get_option( 'wbk_service_label', '' );
$value = htmlspecialchars( $value );
$html = '<input type="text" id="wbk_service_label" name="wbk_service_label" value="'.$value.'" >';
$html .= '<p class="description">' . __( 'Service label', 'wbk' ) . '</p>';
echo $html;
}
// validate service label
public function validate_service_label( $input ) {
$allowed_tags = array(
//formatting
'strong' => array(),
'em' => array(),
'b' => array(),
'i' => array(),
'br' => array(),
//links
'a' => array(
'href' => array(),
'class' => array()
),
//links
'p' => array(
'class' => array()
)
);
$input = wp_kses( $input, $allowed_tags );
return $input;
}
So, in the dashboard options page I use htmlspecialchars function
In frontend page I use like this:
$label = get_option( 'wbk_service_label', __( 'Select service', 'wbk' ) );

How to display custom shortcode single image in visual composer in wordpress?

I am trying to display custom fileds created by me in visual composer by using custom short codes. This custom short codes run fine when i am working with heading and text area_html ,but now i want to add single image in this sort code,but in result i am not getting image,it displays alt attribute and in back-end side i am showing my single image that stores in custom shortcode field. here i am including my code.
1) code for creating custom shortcode
vc_map( array(
'name' => __( 'trionn_header' ),
'base' => 'trionn_header',
'category' => ( 'trionn code' ),
'params' => array(
"type" => "attach_image",
"holder" => "img",
"class" => "",
"heading" => __( "Hintergrundbild", "my-text-domain" ),
"param_name" => "image_url",
"value" => __( "", "my-text-domain" ),
"description" => __( lade eins hoch", "my-text-domain" )
)
) );
2) code in separate function-name file
<?php
/* Ordered List shortcode */
if (!function_exists('trionn_header')) {
function trionn_header($atts, $content) {
$atts = shortcode_atts(
array(
'image_url' => ''
), $atts, 'trionn_header'
);
$imageSrc = wp_get_attachment_image_src($image_url, 'thumbnail');
$html = '<img src="' . $imageSrc[0] .'" alt="' . $atts['title'] . '"/>';
return $html;
}
add_shortcode('trionn_header', 'trionn_header');
}
I found solution for your question,try this in your code
In param tag write this array after main param attribute:
array(
"type" => "attach_image",
"heading" => "Image",
"param_name" => "image",
'admin_label' => true
)
paste below code in your function_name file:
<?php
// Trionn header custom code //
if (!function_exists('trionn_header')) {
function trionn_header($atts, $content = null) {
$args = array(
'title' => __( 'This is the custom shortcode' ),
'title_color' => '#000000',
'content' => 'your discrption here',
"image" => "",
);
extract(shortcode_atts($args, $atts));
//init variables
$html = "";
$image_classes = "";
$image_src = $image;
if (is_numeric($image)) {
$image_src = wp_get_attachment_url($image);
}
// generate output for heading and discription
$html = '<h1 class="trionn header ' . $atts['style']. '" style="color: ' . $atts['title_color'] . '">'. $atts['title'] . '</h1>'. "<div class=content>" . $content . "</div>";
// generate output for single image
$html .= "<img itemprop='image' class='{$image_classes}' src='{$image_src}' alt='' style='{$images_styles}' />";
return $html;
}
add_shortcode('trionn_header', 'trionn_header');
}
Enjoy, thank me later

Manage custom checkout fields in woocommerce

Hi I have a question regardin custom checkout fields in woocommerce.
I created a custom field in the checkout form and everything was working perfectly. The field contains a customer card number. I also managed to save the field value (once entered the first time) in the wp-usermeta, so that it doesn't come along only with the order, but it is saved together with teh customer details.
Now I would like to do the following. Once a registered customer returning to the shop goes to the checkout form, the new field (if not empyt) shows up automatically, instead of asking the customers to insert their card number everytime.
The original functions that I added to my child theme fucntions.php for showing the custom field in the checkout page was:
/**
* Add the field to the checkout
*/
add_action ('woocommerce_after_order_notes', 'checkout_w55_card_number' );
function checkout_w55_card_number( $checkout ) {
echo '<div id="checkout_w55_card_number"><h2>' . __('N. tessera W55') . '</h2>';
woocommerce_form_field( 'w55_card_number', array(
'type' => 'text',
'class' => array('w55-card-number-class form-row-wide'),
'label' => __('Inserisci n. tessera W55'),
'placeholder' => __('Inserisci numero'),
), $checkout->get_value( 'w55_card_number' ));
echo '</div>';
}
I thus tried to re-arrange it in this way, but it doesn't work
add_action ('woocommerce_after_order_notes', 'checkout_w55_card_number' );
function checkout_w55_card_number( $checkout ) {
//extracts the value of the w55_card_number field
$w55_card = get_user_meta($user_id, 'w55_card_number', true);
//condition is the w55_card_number field is empty or not
if ( !empty($w55_card) ) : echo $w55_card; else :
echo '<div id="checkout_w55_card_number"><h2>' . __('N. tessera W55') . '</h2>';
woocommerce_form_field( 'w55_card_number', array(
'type' => 'text',
'class' => array('w55-card-number-class form-row-wide'),
'label' => __('Inserisci n. tessera W55'),
'placeholder' => __('Inserisci numero'),
), $checkout->get_value( 'w55_card_number' ));
echo '</div>';
endif;
}
Any suggestion?
Your user_id variable was not defined. You need to do the following:
add_action ('woocommerce_after_order_notes', 'checkout_w55_card_number' );
function checkout_w55_card_number( $checkout ) {
$user_id = get_current_user_id();
$w55_card = '';
if ($user_id) {
//extracts the value of the w55_card_number field
$w55_card = get_user_meta($user_id, 'w55_card_number', true);
}
//condition is the w55_card_number field is empty or not
if ( !empty($w55_card) ) : echo $w55_card;
else :
echo '<div id="checkout_w55_card_number"><h2>' . __('N. tessera W55') . '</h2>';
woocommerce_form_field( 'w55_card_number', array(
'type' => 'text',
'class' => array('w55-card-number-class form-row-wide'),
'label' => __('Inserisci n. tessera W55'),
'placeholder' => __('Inserisci numero'),
), $checkout->get_value( 'w55_card_number' ));
echo '</div>';
endif;
}

Resources