Categories
Magento 2

Magento 2 – Admin form UI Component ImageUploader Field.

In this article today I will disuse about Magento 2 – admin form UI Component imageUploader field. This article is include all steps UI Components tutorial for image upload from your custom module. UI Component imageUploader xml form field, Show image in edit form, Save image info in database.

Below example code is based on a custom module “Extendfeature_RequestQuote” this is my custom module.

Best article for frontend custom form image uploader
Magento 2 – File or image upload via frontend custom module form and Controller

Step 1: Add UI Component imageUploader field

Please add/create following UI component imageUploader code in your UI form located in Extendfeature/RequestQuote/view/adminhtml/ui_component/form.xml file. My image uploader field name is “filesubmission” show field as “File Attachment” in admin form.

Step 2: Create image-preview.html template file

Please add/create image preview template with following code located in Extendfeature/RequestQuote/view/adminhtml/web/template/image-preview.html file.

Step 3: Crate image upload admin Controller file

Now create a adminhtml image upload Controller Filesubmission.php for image upload in Extendfeature\RequestQuote\Controller\Adminhtml\Requestquote\Filesubmission.php file with following code.

Step 4: Crate image upload Model

Now we create a image upload Model ImageUploader.php, in this we added more methods related upload image into pub/media/requestaquote directory, here “requestaquote” is IMAGE_UPLOAD_DIRECTORY name. Please create a model file with following code.

Step 5: Define Image Uploader configuration in di.xml file

Define Image Uploader configuration in your module di di.xml file with following code, File located in Extendfeature/RequestQuote/etc/di.xml.

Magento 2 - Admin form UI Component ImageUploader Field

Step 6: Image name save into the database field

To save image name into the database field please add follow below code into Save controller Extendfeature/RequestQuote/Controller/Adminhtml/Requestquote/Save.php file inside execute method.

Step 7: Display image after save records in edit form

Now add following code into DataProvider like Extendfeature/RequestQuote/Model/Requestquote/DataProvider.php File to display image after save records inside public function getData() method.

Now, Execute below commands via SHELL and enjoy image uploader function in your UI form.

Thank you for carefully read my article If you have any query please Leave your query on following comment section or contact with us. Please SHARE this post.

Categories
WordPress

How to Create WordPress Custom Post Types?

In this post today I will disuse How to Create WordPress Custom Post Types and displaying with Shortcode and page template. Each new developer need to learn how to easily create custom post types in WordPress site. Also WordPress regular blog posts use Categories and Tags to create an organization structure. In this article I also disuse how to crate separate Categories and Tags for custom post types.

Working With Custom Post Types

So your new custom post types need to effectively create and use, you will need to be familiar with the following:

  • Creating custom Post Types
  • Creating custom Taxonomies
  • Creating custom Tags

Creating Custom Post Types

Let’s start and register custom post type in your theme functions.php file by method register_post_type. It will be located in public_html/wp-content/themes/YOUR_THEME_NAME/functions.php Open this file via editor and add following code to register custom post type.

For more information I will shortly describe some args details in below.

  • labels The labels option should be an array defining the different labels that a custom post type can have. I have separated this out above just to make the arguments for registering a post type clearer.
  • description A short explanation of our custom post type, what it does and why we are using it.
  • public This option controls a bunch of things in one go. Setting this to true will set a bunch of other options (all to do with visibility) to true.
  • menu_position Defines the position of the custom post type menu in the back end. Setting it to “7” places it below the “posts” menu.
  • supports This option sets up the default WordPress controls that are available in the edit screen for the custom post type. By default, only the title field and editor are shown.
  • has_archive If set to true, rewrite rules will be created for you, enabling a post type archive at http://yoursite.com/posttype/ (by default)

Our current custom post types Faqs associated with a custom taxonomy called genres. I also set hierarchical to false If you would like your custom post type to behave like Pages, then you can set this value to true.

Category for Custom Post Type

Now we are going to register taxonomy for custom post type named “Faq Categories” after register_post_type method in functions.php file by register_taxonomy method. This categories we only use for Faq post types.

Registering categories for custom post type is done, We added some labels and set hierarchical option to true. This enables custom “category style” to taxonomies. When we set it to false (this is the default), taxonomy will be like the default tags.

Displaying Custom Post Types by Shortcode

Now I am going to create a shortcode [faqs] to displaying custom post type post items. Please added following code in your theme functions.php in theme public_html/wp-content/themes/YOUR_THEME_NAME/functions.php.

We can pass tow parameter via shortcode “category” Example: [faqs category=”7″] (7) is category ID, Second one is “class” Example: [faqs class=”sidebar-categories”] is CSS class.

Displaying Custom Post Types in Template

Also we can listed custom post via template file, open a template file via editor from your theme public_html/wp-content/themes/YOUR_THEME_NAME/ directory. By querying the database, you can retrieve post items from a custom post type.

So in this post we are completed 3 (three) part, 1. Create Custom Post Types, 2. Categories for Custom Post Types, 3. Displaying Custom Post Items by Shortcode and Template.

Thank you for carefully read my article If you have any query please Leave your query on following comment section or contact with us. Please SHARE this post.

Categories
WordPress

How to Add New Custom Widget Area?

In this post today I will discus How to Add New Custom Widget Area in WordPress theme. Each new developer need to learn WordPress basics, Also need to know how customize your WordPress site.

After creating new themes developer to customize WordPress themes, So need to know how to create custom Widget area and display Widget area in WordPress theme.

Registering a Custom Widget Area:

Let’s start and registering a widget area add following code in your theme functions.php file by method register_sidebar. It will be located in public_html/wp-content/themes/YOUR_THEME_NAME/functions.php Open this file via editor and add following code to registering new Widget area.

After register a new widget area, WordPress automatically display it as an option under Appearance => Widgets in WordPress admin section. My custom Widget name is “Single Page Top Sidebar” and ID is “sidebar_single_top”.

Registering a Custom Widget Area

Display Your Specific Custom Widget Area:

WordPress not automatically display your newly created Widget area on frontend theme. Because we have not yet told to WordPress where to display newly created Widget area. I have create new widget are for post single page sidebar top place widget area.

Now I need to edit the public_html/wp-content/themes/YOUR_THEME_NAME/singular.php file via editor in your theme and add following code in DIV with .sidebar class to display your custom widget area.

Registering a Custom Widget Area

You can create Widget area for any page section like Header, Sidebar, Page Content and Footer with using PHP WordPress conditions.

Example:
is_front_page()
is_single()
is_page()
is_category()
is_singular()

Thank you for carefully read my article If you have any query please Leave your query on following comment section or contact with us. Please SHARE this post.

Categories
WordPress

WordPress – How to Create a Child Theme?

In this post today I will discus, How to Create a Child Theme? Every new developer first need to learn the WordPress basics. Also need to know how customize your WordPress site. Create new child themes is great starting point for developer to customize WordPress themes.

Why Developer Need to Create a Child Theme?

Child themes are considered the best way to customize or extend function your WordPress default or paid themes. A child theme inherits all the features and appearance of its parent theme. Developer can customize without affecting parent theme function.

This allows a developer to easily update parent theme function without losing parent theme new changes. Developer can easily update update parent theme if release new version for parent theme.

Developer Basic Requirements

A developer need to understanding of CSS/HTML/PHP basic, so a developer can make easily own changes. If developer is good at copying and pasting code snippets from other sources, then that would work too. I am recommend a developer to practice on local development environment. Developer can move all local change to live WordPress site. Because on local server developer can dummy content for testing purposes to child theme development.

Creating Your First Child Theme

Now you can chose one of the default WordPress theme can be used as a parent theme. Example: twentytwenty Developer need to open /wp-content/themes/ in your WordPress installation directory and create a new directory (twentytwenty-child) for your child theme. You can name this directory anything you want but good format is like (twentytwenty-child) name.

wordpress-create-child-theme-directory

Go to /wp-content/themes/twentytwenty-child/ directory and create stylesheet file named (style.css) and open with text editor like Notepad or VSCode and your stylesheet must contain the below required header comment at the very top of the file.

Following information is required for child theme:
Theme Name: Needs to be unique to your theme name.
Template: The name of the parent theme directory. The parent theme in our example is the Twenty Twenty theme, so the Template will be twentytwenty. The parent theme folder name is case-sensitive. If you provide with Template – TwentyTwenty, then it will not work.

Now developer need to something change on functions.php file, it’s necessary to add parent theme styles by (parent-style) on child theme and custom stylesheet called by (child-style).

Now developer need to something change on functions.php file, If you want to set logo without cropping logo. Login into wp-admin and go to Appearance => Customize after than click on “Site Identity” try to change logo, select a logo and press Skip Cropping button with following code.

wordpress-create-child-theme-cropping

Activate child theme:

Child theme is ready for active. Please go to WordPress admin and go to menu Appearance => Themes you can find new theme “Twenty Twenty Child” mouse over on new theme and click on “Active” button.

wordpress-create-child-theme-active

Thank you for carefully read my article If you have any query please Leave your query on following comment section or contact with us. Please SHARE this post.