Coder Social home page Coder Social logo

thanhne / vue-wp-restapi Goto Github PK

View Code? Open in Web Editor NEW

This project forked from meuss/vue-wp-restapi

0.0 0.0 0.0 519 KB

Connecting Vue.js to a WordPress website, using axios + the WP REST api

JavaScript 90.37% PHP 0.92% CSS 1.21% HTML 1.13% Vue 6.37%

vue-wp-restapi's Introduction

Connecting Vue.js to a WordPress website, using axios + the WP REST api

A small reminder/checklist for myself in future projects

What I often include in my vue/wordpress projects

Vue.js

WordPress Plugins

  • ACF PRO
  • ACF to REST API
  • Custom Post Type UI
  • Disable Comments
  • Post Types Order
  • WP Migrate DB

How to setup

Load up a new vue project

# go through regular cli install, this is for vue-cli 2
npm init webpack my-project
.....
npm i axios -S
npm run dev

Install WordPress

Download the latest wordPress and unzip it at the root.

Go through the regular local wordpress install:

  • start your local server to it (I use MAMP, localhost:8888)
  • create your local empty database in phpmyadmin
  • visit localhost:8888 and enter wp info
  • install your plugins (if you use ACF, be sure to add ACF to REST API)

Create basic 'theme' for wordpress

  • Delete all default themes (wordpress/wp-content/themes/)
  • Create your own theme, with these necessary files:
    • functions.php
    • index.php
    • screenshot.png (1200x800)
    • style.css
  • Activate the theme in the wp dashboard
  • Don't forget to edit gitignore

Configuration for Vue / webpack

  • config/dev.env.js

      'use strict';
      const merge = require('webpack-merge');
      const prodEnv = require('./prod.env');
    
      module.exports = merge(prodEnv, {
        NODE_ENV: '"development"',
        WP_REST_API: '"http://localhost:8888/wp-json/wp/v2/"',
      });
    
  • config/prod.env.js

      'use strict';
      module.exports = {
        NODE_ENV: '"production"',
        WP_REST_API: '"https://your-production-website.com/wordpress/wp-json/wp/v2/"',
      };
  • src/axios-config.js

      import axios from 'axios';
    
      export const api = axios.create({
        baseURL: process.env.WP_REST_API,
      });

Restart your server with npm run dev

You are now ready to get the data from the api ๐Ÿ˜Ž

Take a look at these components as quick examples:

  • Home.vue (example of getting a page's content)
  • Posts.vue (example of getting latest posts)

=> WP REST API Handbook

vue-wp-restapi's People

Contributors

meuss avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.