Close Menu
Techcolite
    Facebook X (Twitter) Instagram Pinterest
    • Contact Us
    • Privacy Policy
    • Cookie Policy
    • Disclaimer
    Facebook X (Twitter) Instagram Pinterest Tumblr
    TechcoliteTechcolite
    • Home
    • Tech News
      • Computers & Internet
      • Gadgets
      • Tablets & Mobiles
      • Web Hosting
      • Reviews
    • SEO
    • Software
    • WordPress
    • Business
    • Marketing
    • Off Topic
      • Tips & Tricks
    • About Us
    • Write for us
    • Contact Us
    Techcolite
    Home»WordPress»How to use the WordPress Package in Java Script Apps
    WordPress

    How to use the WordPress Package in Java Script Apps

    Team TechcoliteBy Team TechcoliteNovember 23, 2019No Comments6 Mins Read
    Facebook Twitter Pinterest LinkedIn Telegram Tumblr Email
    Share
    Facebook Twitter LinkedIn Pinterest Email

    How to use the WordPress Package in Java Script Apps
    Image Credit:- www.ibenic.com

    If you want to make your plugins or themes extensible, the best way to do it is to use WordPress packages API (hooks). This is when using PHP. What about when you are using JavaScript? To make your JavaScript project extensible in a similar way, this tutorial will show you how to use those hooks in a React App.

    What are @WordPress/hooks?

    Let us first define what JavaScript hooks are. When running a JavaScript app in WordPress and you has activated the Gutenberg Editor, you can still use it by calling it from wp.hooks, without installing anything. Another way to go about it is to include that in your JavaScript app by installing it this way:

    Npm install @WordPress/hooks –save

    How to create your Hook environment

    A hook environment is created by building a global object that will be accessible throughout your entire app. Another way is to use the hook system as an internal object which you’ll pass to components.

    Import {createHooks} from ‘@WordPress/hooks’;

    To start your Hook environment, you can use the create Hooks function this way:

    Varmy Global Hooks = create Hooks ();

    It is that simple and when you are done, you can start creating actions and filters to make your app extensible.

    You can use functions like add action, do action, apply filters, add filter, when you’re used to the WordPress Plugin API. You will, however, notice a small difference when implementing this hook system.

    This difference is brought by the namespace which you also add when adding or removing actions and filters. This is applicable for cases where you have the same function names under different components. You can easily distinguish which component or app it belongs to, using namespaces to avoid removing the wrong function by accident.

    The available actions functions include:

    • addAction( ‘hookName’, ‘namespace’, ‘functionName’, callback, priority )
    • removeAction( ‘hookName’, ‘namespace’, ‘functionName’ )
    • removeAllActions( ‘hookName’ )
    • doAction( ‘hookName’, arg1, arg2, … )
    • doingAction( ‘hookName’ )
    • didAction( ‘hookName’ )
    • hasAction( ‘hookName’ )

    Available filter functions are:

    • addFilter( ‘hookName’, ‘namespace’, ‘functionName’, callback, priority )
    • removeFilter( ‘hookName’, ‘namespace’, ‘functionName’ )
    • removeAllFilters( ‘hookName’ )
    • applyFilters( ‘hookName’, content, arg1, arg2, … )
    • doingFilter( ‘hookName’ )
    • didFilter( ‘hookName’ )
    • hasFilter( ‘hookName’ )

    The string function Name is used to remove the registered callback. However, you can still apply the filter function if it’s not there, but it’s going to be hard removing them later. Another way to view all registered actions and filters through console.log (myGlobalHooks). Here you will also find actions and filters keys. All your actions and filters will be applied here.

    Creating the Extensible React App

    To kick start a simple React App, lets head straight to “Create React App”. To find out how to use it, you can check the repository information on GitHub.

    The first thing you need to do is open your terminal and place it in a folder used on your website or desktop. You then have to create a folder for the app. Remove the my-app name and rename it with a name that suitable for you. The render method in App.js now contains all the JSX that will be rendered. Make that output (JSX) extensible with @WordPress/hooks, so you can combine what you want there.

    Ensure that you are inside the newly created folder when installing @WordPress/hooks package:

    Npm install @WordPress/hooks>>save

    Creating your Hook Environment

    You now need to create a global object that will contain your hook environment.

    Import React, {Component} from ‘react’;

    Import logo from ‘. /logo.svg’;

    Import {create Hooks} from ‘@WordPress/hooks’;

    Import ‘. /App.css’;

     

    letglobalHooks = createHooks();

     

    Class App extends Component {

    // …

    }

    After that, you will then need to implement some actions and filters. This is done by changing the render method. It will have a variable output that is an array. This is then used with apply Filters to have a few positions where we can add JSX to your output array.

    It is clear that you are now using does Action to create a hook where various calls are done before the output even starts. You can later use three apply Filters calls and render your output.

    Rendering the Header output

    You are now using the whole JSX that you were given in the app creation.

    Class App extends Component {

    Constructor ( props ) {

    // …

    globalHooks.addFilter( ‘header_output’, ‘myApp’, this.renderHeader );

    };

     

    /**

    * Rendering the Header

    *

    * @param {array} content

    */

    renderHeader( header ) {

    // Applying filters to change the image. Default is the logo.svg.

    letlogoImage = globalHooks.applyFilters( ‘header_logo’, logo );

     

    letshouldLearnReact = globalHooks.applyFilters( ‘should_learn’, true );

     

    letheader_html = <header key=”header” className=”App-header”>

    <imgsrc={logoImage} className=”App-logo” alt=”logo” />

    <p>

    Edit <code>src/App.js</code> and save to reload.

    </p>

    { shouldLearnReact&&<a

    className=”App-link”

    href=”https://reactjs.org”

    target=”_blank”

    rel=”noopenernoreferrer”

    >

    Learn React

    </a>}

    </header>;

    header.push(header_html );

    return header;

    }

    }

    The constructor method allows you to add the filter to the hook header output, with the namespace my App and with the callback to your method render Header. You will receive an array as a parameter to method since the output is an array.

    You can then apply different filters, including:

    Header logo – you can change the logo

    Should learn – This filter is returning a Boolean. Your header will have a link to ReactJS website. By default, it returns true. /li>

    Once that is passed through your Hook environment, you will then build a JSX output and push into your array and returning the array. While being inside the app folder, you can type now inside the terminal npm start.

    Conclusion

    If you are using WordPress then the @WordPress/hooks package is a powerful tool if you are using filters and actions within your plugins/themes or others. This package is the perfect option to help you build extensible JavaScript applications.

     

    About Author:-

    Naman Modi is a Professional Blogger, SEO Expert & Guest blogger at NamanModi.com, He is an Award-Winning Freelancer & Web Entrepreneur helping new entrepreneur’s launches their first successful online business.


    Discover more from Techcolite

    Subscribe to get the latest posts sent to your email.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Team Techcolite
    • Website
    • Facebook
    • X (Twitter)
    • Pinterest
    • LinkedIn

    Techcolite is about Latest Technology news, Gadgets, Computers, Internet, SEO, Marketing and anything related to day to day technology.

    Related Posts

    Premium Professional WordPress Themes for Tech Bloggers

    March 21, 2025

    WordPress Development Trends to Watch Out for in 2025

    January 20, 2025

    WordPress security: Untangling & fixing the major issues

    December 13, 2023

    15 Best Elementor Themes (and When to Use Them)

    December 9, 2023

    Elevate Your Website with Managed WordPress Hosting

    November 28, 2023

    Best Plugin To Add Google Reviews On WordPress 2023

    September 22, 2023
    Leave A Reply Cancel Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    • Facebook
    • Twitter
    • Pinterest
    • Instagram
    • Tumblr
    • Mastodon
    InmotionhostingInmotionhosting
    NamecheapNamecheap
    DigitaloceanDigitalocean
    Advertisement
    LiquidwebLiquidweb
    Join 1000+ Subscribers

    Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    hubspothubspot
    About Techcolite

    TechColite.com is a dynamic tech blog offering in-depth insights and analysis on the latest trends in technology, gadgets, software, and digital innovations. With a focus on providing accessible yet comprehensive content, TechColite covers a wide array of topics, including AI, cloud computing, cybersecurity, app development, and emerging tech. Whether you’re a tech enthusiast, a developer, or a business leader, TechColite delivers expert reviews, tutorials, and industry news to keep you informed and ahead of the curve. The blog is dedicated to helping readers navigate the fast-paced world of technology with clarity and confidence.

    Partners
    DMCA.com Protection Status

    Web Safety

    BOSS

    techcolite.com

    Free of toxic links

    Approved by Sur.ly

    2022

    Discover latest Indian Blogs
    Mastodon
    Listed On
    Copyrighted.com Registered  Protected
    “Top
    DMCA Compliance
    Top 90 Indian Technology Blogs
    Copyright Notice

    © Techcolite.com, 2015 to 2025. Unauthorized use and/or duplication of this material without express and written permission from this site’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to Techcolite.com with appropriate and specific direction to the original content.

    Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
    To find out more, including how to control cookies, see here: Cookie Policy
    Facebook X (Twitter) Instagram Pinterest Tumblr
    • Contact Us
    • Privacy Policy
    • Cookie Policy
    • Disclaimer
    Copyright © 2025 All Rights Reserved. Techcolite.com.

    Type above and press Enter to search. Press Esc to cancel.