Add the custom widget value to the array on wordpress website - php

We have a home page banner in our WordPress website. Added a logic to show different images on page refresh.
The code for the images are placed in the help_functions.php, updated the file and it worked as expected.
$images = array( "https://images.pexels.com/photos/325185/pexels-photo-325185.jpeg?auto=compress&cs=tinysrgb&dpr=3&h=750&w=1260", "https://images.pexels.com/photos/3787839/pexels-photo-3787839.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940", "https://images.pexels.com/photos/3769312/pexels-photo-3769312.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940")
$rand_img = array_rand($images, 1);
$image =$images[$rand_img];
Instead of hardcoding, I register a new custom widget by adding below code in the functions.php
register_sidebar(array(
'name' => esc_html__('Splash images Widget', 'wpresidence'),
'id' => 'splashimage-widget-area',
'description' => esc_html__('The splash image widget area', 'wpresidence'),
'before_widget' => ' ',
'after_widget' => ' ',
'before_title' => ' ',
'after_title' => ' ',
));
add_action( 'widgets_init', 'register_my_widgets' );
and I added the custom html to the widget and added the below code
array( "https://images.pexels.com/photos/325185/pexels-photo-325185.jpeg?auto=compress&cs=tinysrgb&dpr=3&h=750&w=1260", "https://images.pexels.com/photos/3787839/pexels-photo-3787839.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940", "https://images.pexels.com/photos/3769312/pexels-photo-3769312.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940")
And tried updating the code in help_functions.php file as below
$images = dynamic_sidebar( 'splashimage-widget-area' );
$rand_img = array_rand($images, 1);
$header_type=20;
$image =$images[$rand_img];
Its not working as expected.
Expected result, Add images link in the widget and update the help_functions.php file to show the different images on refresh.

I register a new custom widget
You are registering an area for the widgets using register_sidebar(), not the widget itself.
This widget area allows you to assign widgets (custom HTML, image, recent posts etc) into that area and specify where you want them to appear in your template files.
To register a widget you need to use register_widget() function and class, see this tutorial on how to create your first widget.
However, if you don't want to register new widget you can register shortcode that outputs what you want, then add it via a custom html/text widget.
Registering shortcode is very easy using add_shortcode() function, especially if you plan to hardcode an array of images and not pass any attributes.
More on registering shortcode here.

Related

Swap Wordpress "Widget Area" based on Page Template

i'm using 3 widget areas in my child theme's footer.
i created a secondary custom theme successfully, and am hoping i can simply change those 3 widget area's out in the second theme.
"Footer - Column 1" would change to "Alt Footer - Column 1"
"Footer - Column 2" would change to "Alt Footer - Column 2"
"Footer - Column 3" would change to "Alt Footer - Column 3"
(and yes, i've already created in WP admin > Appearnace > Widgets, those 3 new custom widget areas, & put temp text widgets in them for now).
i'm using this in "functions.php" to change the "Menu"...
add_filter( 'wp_nav_menu_args', 'respect_menu_swap' );
function respect_menu_swap( $args = '' )
{
if(is_page_template('template-respect.php') && $args['menu_id'] == 'avia-menu')
{
$args['menu'] = '16';
}
return $args;
}
...am hoping for something similar to change each of those "widget areas".
i've searched, read, searched, & read a LOT trying to figure this out & just can't seem to grasp what should go in place of the menu terms in that code bit. i really struggle with PHP, so would greatly appreciate specific explanation & code:-)
and, i'm saying "Widget Area" instead of "Widgets" because i realized that "Widgets" are INSIDE the "Widget Areas". i'd like to swap the whole area instead of just the widget so my people can add/remove various widgets in those 3 "areas" in the WP > Appearance > Widgets admin page as needed. it’s my understanding that if i just use the “Widget ID” then when someone changes which widget(s) are in one of those widget areas, it won’t update on the sites front-end without me changing those ID’s first. i’d like to avoid having to do that if possible.
(BTW: i'm using the Enfold WP theme, if that matters)
WOW!
well, it would seems as tho i figured it out!
after MORE searching, i came across https://learn.wordpress.org/lesson-plan/widget-areas
apparently i had to "register" a "sidebar" - which thru me, as i was wanting to modify in the footer. after looking that up, the term "sidebar" is used anywhere the theme allows the user to add widgets in. and since the Enfold theme footer allows for widgets, i had to "register_sidebar" and THEN modify a line in the template.
i also didn't understand why i had to "register" the "widget area's" at all, thinking that just by creating the custom widgets in WP admin > Appearance > Widgets, that should do it, but no. ya gotta create them there and ALSO register them in the child functions too.
so after reading that wordpress page (linked above), i started hunting around in the main Enfold theme files, trying to find something similar. since apparently, some things are named differently than in other themes, which is why my copy/pasting of examples i found elsewhere didn't work.
i found it in... themes > enfold > includes > admin > register-widget-area.php
so i combined what that wordpress link offered with the footer widget registration and came up with this code that i put in my child functions.php...
add_action( 'widgets_init', 'MYWIDGETAREA' );
function MYWIDGETAREA()
{
register_sidebar( array(
'name' => 'Respect Footer - Column ' . $i,
'id' => 'espect_foot_widget_' . $i,
'before_widget' => '<section id="%1$s" class="widget clearfix %2$s">',
'after_widget' => '<span class="seperator extralight-border"></span></section>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>'
));
}
in the array, i only changed the "name" & "id", leaving the rest so the structure matches the original theme.
then i went to my template-custom.php and found this line...
if( ! ( function_exists( 'dynamic_sidebar' ) && dynamic_sidebar( 'Footer - Column ' . $i ) ) )
...and replaced it with...
if( ! ( function_exists( 'dynamic_sidebar' ) && dynamic_sidebar( 'Respect Footer - Column ' . $i ) ) )
AMAZING!!!
well hopefully all this may benefit someone else in a similar position some day.
If you just want a different widget area for each page template, you can just register a widget area for each one, since the page template would contain it.
But, if you're using the same footer file on multiple page templates and want to load different widget areas dynamically, you would first have to register widget areas with the page template slugs included in the ID, for example:
function custom_register_widget_area() {
register_sidebar( array(
'name' => 'Template Respect Footer',
'id' => 'template-respect-footer',
'before_widget' => '<div class="widget">',
'after_widget' => '</div>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'custom_register_widget_area' );
Adding another register_sidebar() for each template.
Then in the footer file (using template-respect.php from your example) you can get the basename of the template to build the widget area ID:
$template = basename( get_page_template_slug( get_the_id() ), '.php' );
if ( $template && is_active_sidebar( $template . '-footer' ) ) {
dynamic_sidebar( $template . '-footer' );
} elseif ( is_active_sidebar( 'default-footer' ) ) {
dynamic_sidebar( 'default-footer' );
}
This would load whichever widget area matches the page template slug, or the default if the default page.php is being used.

How can I create a Custom Text Box on a WooCommerce Product Page?

I am working on a WordPress website, with WooCommerce functionality.
I have created 2 Custom Fields, for the Product Data box within the backend of the Product Page, using the following Code:
<?php
function theme_name_woocommerce_custom_fields() {
// Price Per Character
woocommerce_wp_text_input(
array(
'id' => '_character_price',
'label' => 'Price Per Character',
'description' => 'This is the amount a customer pays per letter entered.',
'desc_tip' => 'true',
'placeholder' => 'Enter Amount per Letter (Exclude the £)'
)
);
// Custom Text Box
woocommerce_wp_checkbox(
array(
'id' => '_custom_text_box',
'label' => 'Show Custom Text Box',
'description' => 'Select this box, if you would like a Custom Text Box to appear on the Product's page.',
'desc_tip' => 'true',
)
);
}
add_action( 'woocommerce_product_options_general_product_data', 'theme_name_woocommerce_custom_fields' );
function save_theme_name_woocommerce_custom_field( $post_id ) {
if ( ! empty( $_POST['_custom_text_field'] ) ) {
update_post_meta( $post_id, '_custom_text_field', esc_attr( $_POST['_custom_text_field'] ) );
}
}
add_action( 'woocommerce_process_product_meta', 'save_theme_name_woocommerce_custom_fields' );
?>
In reference to the woocommerce_wp_checkbox, I would like to create a function whereby when this checkbox is selected, it creates a Custom Text Box on the associated Product's page. This Custom Text Box can then be used by a potential customer to enter a piece of text, which they would like to have printed to the page's Product.
Is anyone aware of what additional piece of coding I would need to enter, in order to achieve said goal?
You can override simple/variable product's template file and add custom field there. Keep logic of show/hide textbox when checkbox is checked there only.
When add to cart is done, you will get all posted data in POST. Grab it from there and put into woocommerce' object.
=======================================================
Edited:
You can follow this steps:
Copy woocommerce/templates/single-product/add-to-cart/variable.php this template to your child theme and customize it. You will see a form tag there. In that you can put your custom created checkbox(which you added from admin). Also, add a custom textbox(in which user will enter text) - and hide it.
Add custom js from functions.php. - In this js you can write logic that if checkbox is checked then you will show the textbox, else not.
Now, when user does add to cart, add this custom textbox data to woocommerce object. How to enter custom data to woocommerce object - you can have step by step details here: https://wisdmlabs.com/blog/add-custom-data-woocommerce-order/

How to add wordpress image caption to woocommerce archive product (shop) page but not to the single product page

I am using Woocommerce 2.4.4 and Wordpress 4,3, I have read many suggestions for making changes to woocommerce shop page; however I cannot find anyone who addresses my specific request. I have limited understanding of Woocommerce; however I have been trying to solve my request, but to no avail.
I want to display the wordpress caption field for each image in the wordpress media library in my woocommerce shop page but not on the individual products.
I have made changes to my child theme, Deli (A woocommerce theme) functions.php . I added the following to the child functions.php.
function wp_get_attachment( $attachment_id ) {
$attachment = get_post( $attachment_id );
return array(
'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt',
true ),
**'caption' => $attachment->post_excerpt,**
'description' => $attachment->post_content,
'href' => get_permalink( $attachment->ID ),
'src' => $attachment->guid,
'title' => $attachment->post_title
);
}
I believe the field that has the image caption is 'caption' => $attachment->post_excerpt, as defined above.
I am trying to edit the Woocommerce Archive-product.php but do not know what to add or where to add it in the archive-product.php.
Hey as per your requirement have tried a code which might help you in your case or you can customize it further as per your request.
Add the following code to your Theme's functions.php.
Note: Its better to create a child theme first and do your customization in it so that the customization won't get overwritten when the theme updates.
add_filter( 'wp_get_attachment_image_attributes','wdm_shop_image_caption_func', 10,3 );
function wdm_shop_image_caption_func($attr, $attachment, $size){
if($size=='shop_single'){
unset($attr['title']);
}
elseif($size=='shop_catalog'){
$attr['title']=$attr['alt'];
}
return $attr;
}
Make sure that you set Caption for every Product's Images.

Add key values in a array from outside of array

This is a php question, that happens to involve Wordpress.
My Wordpress parent theme adds a custom metabox. I need to add additional options to the metabox but I need to add the key values options from within my child theme...not in the parent theme where the array is created.
Adding the additional options (key values) in the parent theme is simple, but core theme files have to be changed and therefore will break when the partent theme is updated by the theme developer.
So I have made a child theme that handles all my custom adds...but I have no clue how to inject key vales into an array that is crated in the parent theme.
Here is an excerpt from the the array that builds the options for the metabox (parent theme):
$page_meta_boxes = array(
"Page Item" => array(
'item'=>'page-option-item-type' ,
'size'=>'page-option-item-size',
'xml'=>'page-option-item-xml',
'type'=>'page-option-item',
'name'=>array(
'home-page-featured'=>array(
'main-title'=>array(
'title'=> 'MAIN TITLE',
'name'=> 'page-option-item-featured-text-title',
'type'=> 'inputtext'),
'main-caption'=>array(
'title'=> 'MAIN CAPTION',
'name'=> 'page-option-item-stunning-text-caption',
'type'=> 'textarea'),
),
)
),
)
I want to add additional options to the metabox such as:
'get-started-button-title'=>array(
'title'=> 'GETTING STARTED BUTTON TITLE',
'name'=> 'page-option-item-featured-text-button-title',
'type'=> 'inputtext',
'description'=> 'The stunning text button will appear if this field is not a blank.'),
'get-started-button-link'=>array(
'title'=> 'GETTING STARTED BUTTON LINK',
'name'=> 'page-option-item-featured-text-button-link',
'type'=> 'inputtext',
'description'=> 'This is a stunning text button link url. This field will be ignored when button title equals to blank.'),
Is this even possible?
UPDATE:
What I have tried so far
I am including a file called options.php from within my child theme but its not adding the additional getting-started-button option.
$page_meta_boxes['Page Item']['name']['home-page-featured']['get-started-button-title'] = array(
'title'=> 'GETTING STARTED BUTTON LINK',
'name'=> 'page-option-item-featured-text-button-link',
'type'=> 'inputtext',
'description'=> 'This is a stunning text button link url. This field will be ignored when button title equals to blank.'
);
You could add additionnal options like this :
$page_meta_boxes['Page Item']['name']['home-page-featured']['get-started-button-title'] = array(....);
Be aware that even if you don't change the core theme. You rely on the way it is structured. So your code might still break in case of an update by the theme developer.

Wordpress Navigation Help

I am working on a site that calls the categories of a wordpress page and displays them in the right-side navigation using a php call. I am new to php and web programming in general. Is there a way I could split the categories into two sections using a particular php call or perhaps an if-loop.
Essentially, I want to display particular categories under custom sub-headings to better organize the site. Any help, I'm currently using the following script to display my categories:
<ul><?php wp_list_categories('show_count=1&title_li='); ?></ul>
Here is my site for reference: http://www.merrimentdesign.com
Try using your code above twice. Each time, you can use the other function arguments to limit the output to certain categories. See http://codex.wordpress.org/Template_Tags/wp_list_categories for the various ways to customize the output of the function.
For example you could use:
<ul><?php wp_list_categories('show_count=1&title_li=&child_of=100'); ?></ul>
// where 100 is the parent id of all of the categories you want to print.
<ul><?php wp_list_categories('show_count=1&title_li=&exclude_tree=100'); ?></ul>
// and then show everything, but children of 100
Or simply use the first string multiple times specifying different parent ids each time.
By far and away your best option is to use the new menu functionality within WordPress. It's dead straight forward to set up in your theme:
add_theme_support( 'menus' );
add_action( 'init', 'register_my_menus' );
function register_my_menus() {
register_nav_menus(
array(
'public-menu' => __( 'Public Menu' ),
'sidebar-public-menu' => __( 'Sidebar Public Menu' ),
'sidebar-members-menu' => __( 'Sidebar Members Menu' ),
'sidebar-staff-menu' => __( 'Sidebar Staff Menu' ),
'footer-menu' => __( 'Footer Menu' )
)
);
}
place that in your functions.php file (and obviously change it for your requirements).
Then in your template file - probably sidebar.php you'll want something like:
<?php wp_nav_menu( array( 'theme_location' => 'sidebar-staff-menu', 'container' => false ) ); ?>
And then go to the back end of WordPress (your wp-admin) and then go to Appearance > Menus and voila you're able to drag and drop your categories to your heart's content!
Helpful link: http://justintadlock.com/archives/2010/06/01/goodbye-headaches-hello-menus
Read that, Justin Tadlock is awesome.
Good luck.

Categories