સૌપ્રથમ, જો તમારી પાસે પહેલેથી લોકલ વર્ડપ્રેસ ઇન્સ્ટોલેશન ન હોય, તો તમારે તે ઇન્સ્ટોલ કરવું પડશે. એ માટે, તમે શરૂઆત પેજ પરની સૂચનાઓને અનુસરી શકો છો. વૈકલ્પિક રીતે, તમે ડેવલપમેન્ટ એન્વાયરમેન્ટ ટ્યુટોરીયલમાં આપેલા પગલાં અનુસરી શકો છો. એકવાર તમારું સેટઅપ પૂર્ણ થાય, તો પછી તમે આ ટ્યુટોરીયલના આગળના ભાગને વધી શકો છો.
અમે અમારી એપ્લિકેશનને એક વર્ડપ્રેસ પ્લગઇન તરીકે બનાવશું. જો તમને ખબર નથી કે પ્લગઇન શું છે, તો આગળ વધતા પહેલા ઓછામાં ઓછું પરિચય અને વર્ડપ્રેસ પ્લગઇન્સની મૂળભૂત બાબતો વાંચીને શરૂઆત કરો.
છેલ્લે, આ કોર્સ Redux ના state, actions, and selectors જેવા ખ્યાલો પર ખૂબ જ આધાર રાખશે. જો તમે આ ખ્યાલોથી પરિચિત ન હો, તો પહેલા Getting Started With Redux ટ્યુટોરીયલની સમીક્ષા કરીને શરૂઆત કરી શકો છો.તમારા માટે લાભદાયી રહેશે.
નવું પ્લગઇન બનાવવું
આપણે સમગ્ર ડેવલપમેન્ટ વર્ડપ્રેસ પ્લગઇનની અંદર જ કરશું.ચાલો સૌથી પહેલા તમારા લોકલ વર્ડપ્રેસ એન્વાયરમેન્ટમાં wp-content/plugins/my-first-gutenberg-app ડિરેક્ટરી બનાવીને શરૂ કરીએ.આ ડિરેક્ટરીની અંદર આપણે ચાર ફાઇલો બનાવવાની જરૂર પડશે:
my-first-gutenberg-app.php– નવા એડમિન પેજ બનાવવા માટેsrc/index.js– આપણી JavaScript એપ્લિકેશન માટેstyle.css– નવી સ્ટાઇલશીટ માટેpackage.json– બનાવટ પ્રક્રિયા માટે
આગળ વધો અને નીચે આપેલા કોડ સ્નિપેટ્સનો ઉપયોગ કરીને આ ફાઇલો બનાવો:
src/index.js:
import { render } from '@wordpress/element';
function MyFirstApp() {
return <span>Hello from JavaScript!</span>;
}
window.addEventListener(
'load',
function () {
render(
<MyFirstApp />,
document.querySelector( '#my-first-gutenberg-app' )
);
},
false
);
style.css:
.toplevel_page_my-first-gutenberg-app #wpcontent {
background: #fff;
height: 1000px;
}
button .components-spinner {
width: 15px;
height: 15px;
margin-top: 0;
margin-bottom: 0;
margin-left: 0;
}
.form-buttons {
display: flex;
}
.my-gutenberg-form .form-buttons {
margin-top: 20px;
margin-left: 1px;
}
.form-error {
color: #cc1818;
}
.form-buttons button {
margin-right: 4px;
}
.form-buttons .components-spinner {
margin-top: 0;
}
#my-first-gutenberg-app {
max-width: 500px;
}
#my-first-gutenberg-app ul,
#my-first-gutenberg-app ul li {
list-style-type: disc;
}
#my-first-gutenberg-app ul {
padding-left: 20px;
}
#my-first-gutenberg-app .components-search-control__input {
height: 36px;
margin-left: 0;
}
#my-first-gutenberg-app .list-controls {
display: flex;
width: 100%;
}
#my-first-gutenberg-app .list-controls .components-search-control {
flex-grow: 1;
margin-right: 8px;
}
my-first-gutenberg-app.php:
<?php
/**
* Plugin Name: My first Gutenberg App
*
*/
function my_admin_menu() {
// Create a new admin page for our app.
add_menu_page(
__( 'My first Gutenberg app', 'gutenberg' ),
__( 'My first Gutenberg app', 'gutenberg' ),
'manage_options',
'my-first-gutenberg-app',
function () {
echo '
<h2>Pages</h2>
<div id="my-first-gutenberg-app"></div>
';
},
'dashicons-schedule',
3
);
}
add_action( 'admin_menu', 'my_admin_menu' );
function load_custom_wp_admin_scripts( $hook ) {
// Load only on ?page=my-first-gutenberg-app.
if ( 'toplevel_page_my-first-gutenberg-app' !== $hook ) {
return;
}
// Load the required WordPress packages.
// Automatically load imported dependencies and assets version.
$asset_file = include plugin_dir_path( __FILE__ ) . 'build/index.asset.php';
// Enqueue CSS dependencies.
foreach ( $asset_file['dependencies'] as $style ) {
wp_enqueue_style( $style );
}
// Load our app.js.
wp_register_script(
'my-first-gutenberg-app',
plugins_url( 'build/index.js', __FILE__ ),
$asset_file['dependencies'],
$asset_file['version']
);
wp_enqueue_script( 'my-first-gutenberg-app' );
// Load our style.css.
wp_register_style(
'my-first-gutenberg-app',
plugins_url( 'style.css', __FILE__ ),
array(),
$asset_file['version']
);
wp_enqueue_style( 'my-first-gutenberg-app' );
}
add_action( 'admin_enqueue_scripts', 'load_custom_wp_admin_scripts' );
package.json:
{
"name": "my-first-gutenberg-app",
"version": "1.0.0",
"private": true,
"description": "My first Gutenberg App.",
"author": "The WordPress Contributors",
"license": "GPL-2.0-or-later",
"keywords": [ "WordPress", "block" ],
"homepage": "https://github.com/WordPress/gutenberg-examples/",
"repository": "git+https://github.com/WordPress/gutenberg-examples.git",
"bugs": {
"url": "https://github.com/WordPress/gutenberg-examples/issues"
},
"main": "build/index.js",
"devDependencies": {
"@wordpress/scripts": "^18.0.1"
},
"scripts": {
"build": "wp-scripts build",
"format": "wp-scripts format",
"lint:js": "wp-scripts lint-js",
"packages-update": "wp-scripts packages-update",
"start": "wp-scripts start"
}
}
બનાવટ પાઇપલાઇન સેટ કરવી
આ ટ્યુટોરીયલ એવું માની લેશે કે વાચક ESNext સિન્ટેક્સ અને બિલ્ડ ટૂલ્સ (જેમ કે વેબપેક) ના ખ્યાલથી પરિચિત છે. જો તે મૂંઝવણભર્યું લાગે, તો તમે પહેલા Getting Started with JavaScript Build Setup ની સમીક્ષા કરી શકો છો.તે યોગ્ય રહેશે.
બિલ્ડ ટૂલ ઇન્સ્ટોલ કરવા માટે, તમારા ટર્મિનલનો ઉપયોગ કરીને પ્લગઇન ડિરેક્ટરી પર જાઓ અને npm install ચલાવો.
એકવાર બધી ડિપેન્ડન્સી સ્થાપિત થઈ જાય, પછી ફક્ત npm start ચલાવવાનું બાકી રહે છે અને બસ! ટર્મિનલમાં એક વોચર ચાલશે. પછી તમે તમારા ટેક્સ્ટ એડિટરમાં એડિટ કરી શકો છો; દરેક સેવ પછી, તે આપમેળે બિલ્ડ થશે.
તે કામ કરે છે કે નહીં તે ચકાસી રહ્યા છીએ
જો તમે હવે પ્લગઇન્સ પેજ પર જશો, તો તમને My first Gutenberg App નામનું પ્લગઇન દેખાશે. આગળ વધો અને તેને સક્રિય કરો. My first Gutenberg app નામનું એક નવું મેનૂ આઇટમ દેખાશે. એકવાર તમે તેના પર ક્લિક કરશો, પછી તમને એક પેજ દેખાશે જે JavaScript માંથી Hello! લખેલું હશે:

અભિનંદન! તમે હવે એપ્લિકેશન બનાવવાનું શરૂ કરવા માટે તૈયાર છો!