Trying to develop a simple shortcode - wordpress

Basically, I'm trying to do a simple shortcode wherein the user can display a fixed size of an image regardless of the size. For Example [gallery] image [/gallery] it will be displayed like this
http://i255.photobucket.com/albums/hh140/testament1234/thumb_zps5820cef3.png
I tried coming up with my own code but looks like my codes are wrong. I'm not familiar with PHP or WordPress coding yet. I know things like this can be done using plugins but I would rather learn how to code
function image_gallery($atts, $content=null) {
extract(shortcode_atts(array(
'width' => 400,
'height' => 200,
), $atts));
return '<div class="gallery"></div>';
}
add_shortcode('gall', 'image_gallery' );
the styles was provided via style.css

function image_gallery($atts, $content=null) {
extract(shortcode_atts(array(
'width' => 400,
'height' => 200,
'image' => ''
), $atts));
return '<div class="gallery"><img src="'.$image.'" width="'.$width.'" height="'.$height.'" /></div>';
}
add_shortcode('gall', 'image_gallery' );
activate the plugin
in your post or page add this shortcode [gall image="path_to_your_image"]

Related

How to create a filter hook that will change the page title in WordPress

I'm attempting to write a filter that changes the page title from a h5 to a h1.  Here's an excerpt of the parent theme function that I'm attempting to change:
function constico_site_breadcrumbs() {
$breadcrumbs_settings = apply_filters( 'constico_breadcrumbs_settings', array(
'wrapper_format' => '<div class="container"><div class="row">%1$s<div class="breadcrumbs__items">%2$s</div></div></div>',
'page_title_format' => '<div class="breadcrumbs__title"><h5 class="page-title">%s</h5></div>',// trying to change this into h1
'separator' => '/',
'show_title' => $breadcrumbs_page_title,
'path_type' => $breadcrumbs_path_type,
I tried copying the entire function into the filter hook with changes, but it produced errors.
function tt_seopagetitle() { // function with changes } add_filter('constico_breadcrumbs_settings','tt_seopagetitle'); guidance will be much appreciated!
You can overwrite just the page_title_format like this
function tt_seopagetitle($title){
$title['page_title_format'] = '<div class="breadcrumbs__title"><h1 class="page-title">%s</h1></div>';
return $title;
}
add_filter('constico_breadcrumbs_settings', 'tt_seopagetitle', 10)

How can I make the user change any image on my wp theme just from the theme editor?

How can I make the user change any image on my wp theme just from the theme editor?
For example: I have a background image on my theme footer that i hard-coded, I want to give the user(admin) the ability to change it from the theme editor, and thanks on advanced
I don't want to use something like this:
<div class="footer-background background-image" style="background-image: url(<?php echo get_theme_file_uri( 'assets/images/bg.jpg' ) ?>)" >
If you can just give me a wp codex on this, it would be more than helpful, because I couldn't find any thing related to my problem on Google.
So, you could do something like that (in functions.php) :
add_action( 'customize_register', function( $wp_customize ) {
$wp_customize->add_section(
'section_img',
array(
'title' => 'Images',
'priority' => 35,
)
);
$wp_customize->add_setting(
'footer_image'
);
$wp_customize->add_control(
new WP_Customize_Image_Control(
$wp_customize,
'footer_image',
array(
'label' => 'Footer image',
'section' => 'section_img',
'settings' => 'footer_image'
)
)
);
});
And you get the value by doing (in footer.php) :
get_theme_mod('footer_image');
If I were you, I do followings
Install ACF plugin
Create an option Page
Create new custom field (image field) and assign it to previously created options page.
Update footer template to show the image from back end like this
Update footer image src to get back end ACF field value.
https://www.advancedcustomfields.com/resources/options-page/
https://www.advancedcustomfields.com/resources/image/

Register widget area above posts in WordPress Genesis child theme

I have a Genesis child theme that I'm working on (here's my test site) and I can't seem to figure out how to register a widget area above the posts. I see a lot of tutorials (like this one) that show how to add one that spans both columns (posts & sidebar), but I'd like to have one that is only above the posts, not the sidebar.
Looking at these hooks, it would be in the genesis_before_loop hook I believe. I tried tweaking the code in the tutorial I linked to but it didn't work out. Does anyone have the code or link to a better tutorial?
This is code I'm trying to use but it's not working:
/**
* Add widget above posts.
*/
//* Add widget above posts
add_action( 'genesis_loop’, ‘above_posts’ );
function above_posts() {
if (is_active_sidebar( ‘aboveposts’ )) {
genesis_widget_area( ‘aboveposts’, array(
'before' => '<div class=“aboveposts widget-area"><div class="wrap"',
'after' => '</div></div>'
) );
}
}
//* Register widget above posts areas
genesis_register_sidebar( array(
'id' => ‘aboveposts’,
'name' => __( ‘Above Posts’ ),
'description' => __( 'This is the above posts widget.’ ),
) );

Giving a widget my own id or class in Wordpress

I'm writing a Wordpress plugin with a widget. In my jQuery code i'd like to point to that widget, so I though giving it an HTML ID would be nice. (Edited, thanks for pointing out.)
After doing some searching on the internet (and the Codex) I know that I can give ID's to the widgets in the theme, but it's not what I was looking for. This method has flaws. Changing the theme may cause errors (of course, I know it has to be changed in the functions.php, but it's just meh). The other thing is that my widget got a number which may change without me knowing.
So to be 100% sure it works and will work in the future, can I give my widget my own ID?
I might not understand your question exactly and what "ID" do you mean ( widget ID , or actual HTML div ID - which are actually one and the same.. ) but If you have read the codex , the example for how to give an ID is given there ..
function __construct() {
parent::__construct(
'foo_widget', // Base ID
__('Widget Title', 'text_domain'), // Name
array( 'description' => __( 'A Foo Widget', 'text_domain' ), ) // Args
);
}
Another way to do the same ( and helpul if you are talking about HTML elements like divs - you can assign a class )
function My_Widget() {
function My_Widget() {
$widget_ops = array( 'classname' => 'example', 'description' => __('A widget that displays nothing ', 'example') );
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'example-widget' );
$this->WP_Widget( 'example-widget', __('Example Widget', 'example'), $widget_ops, $control_ops );
}
Note that a numerator will be automatically added to your widget´s ID based on how many instances were initiated like :
foo_widget
foo_widget-2
foo_widget-3
etc ...
EDIT I - after comments
At any rate , IMHO it is a bad idea to hard-code a fixed ID in a widget for the simple reason that the preference for a widget from the developer point of view is to always allow support for multiple instances . Giving an an HTML ID anywhere on the widget will cause validation errors AND in the case of jQuery - also JS errors for the simple cause that if a user will have 2 widgets , it will also have a duplicated ID .
In other words - It is the exact opposite of your statement in the original question.
So to be 100% sure it works and will work in the future, can I give my
widget my own ID
Giving a fixed hard coded ID to your widget will in fact ensure that it will NOT work 100% of the time.
The preference is always to target such issues with a class ( or with something like div[id^="my_widget_id"] ) and let wordpress "do it´s thing" ( by auto incrementing IDs ).
For the exact same reason - a theme should always have the same structure in the register sidebar() function like so :
<?php $args = array(
'name' => __( 'Sidebar name', 'theme_text_domain' ),
'id' => 'unique-sidebar-id',
'description' => '',
'class' => '',
'before_widget' => '<li id="%1$s" class="widget %2$s">', // LOOK AT THIS LINE
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>' ); ?>
This will permit the specific sidebar to auto increment the ID of the widgets in order to avoid the above mentioned problem .
From the codex :
before_widget - HTML to place before every widget(default: '') Note: uses sprintf for variable
substitution
All that being said, and if you are insisting of giving a hard-coded fixed ID somewhere there ( instead of a the methods described above ) you can always put that at a nested divor span INSIDE your widget´s HTML output, But I would think that if you have read attentively this answer - you will avoid it now.
Now,- since you have not included any code in your question ( which is always a bad practice here on SE ) there is little more I can do to help. If you encounter any problems targeting the widget without an ID - I suggest you to open a new question and maybe point a link at the comments here, so myself ( and others ) can help you with it ..

Multiple Featured Images Wordpress

So my aim is to find a method of adding more thumbnails only for displaying on a custom post type, for example I wish to have a large image (not the same image) for a featured post and a different image for the default view.
In the end i followed this tutorial and it did exactly what i required to a T.
http://www.lifeonlars.com/wordpress/how-to-add-multiple-featured-images-in-wordpress
have you try this add_image_size
why don't you use custom post template plugin
I got a solution from online. I also customized some code. You can check this.
Step 1 Download this library from this link and put beside functions.php ( theme root ).
Step 2: Copy this code below to functions.php.
/*
* Code for Multiple Featured Image.
* Multiple Featured image is only for your selected post type.
*/
require_once('library/multi-post-thumbnails.php');
if (class_exists('MultiPostThumbnails')) {
new MultiPostThumbnails(array(
'label' => '2nd Feature Image',
'id' => 'feature-image-2',
'post_type' => 'your_post_type_name'
)
);
new MultiPostThumbnails(array(
'label' => '3rd Feature Image',
'id' => 'feature-image-3',
'post_type' => 'your_post_type_name'
)
);
new MultiPostThumbnails(array(
'label' => '4th Feature Image',
'id' => 'feature-image-4',
'post_type' => 'your_post_type_name'
)
);
};
Step 3 Check now.
I can write entire code here, but clicking on this tutorial link is much easier :)

Resources