-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
1,976 additions
and
2 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+6 KB
5 - Using the WP REST API Inside WP/5.4-save-posts-completed/assets/.DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions
3
5 - Using the WP REST API Inside WP/5.4-save-posts-completed/assets/css/custom.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/* | ||
Add Custom Styles Here | ||
*/ |
Binary file added
BIN
+6 KB
5 - Using the WP REST API Inside WP/5.4-save-posts-completed/assets/js/.DS_Store
Binary file not shown.
69 changes: 69 additions & 0 deletions
69
5 - Using the WP REST API Inside WP/5.4-save-posts-completed/assets/js/helpers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const mainContainer = document.getElementById( 'content' ), | ||
appContainer = document.getElementById( 'list-posts' ), | ||
newPostBtn = document.querySelector( '.toggle-add-form' ), | ||
newPostForm = document.getElementById( 'add-post-form' ), | ||
formTitle = document.getElementById( 'new-title' ), | ||
savePostBtn = document.getElementById( 'save-post' ); | ||
|
||
|
||
function togglePostForm( event ) { | ||
|
||
let hidden = false; | ||
|
||
if ( event ) event.preventDefault(); | ||
|
||
hidden = newPostForm.classList.toggle( 'hidden' ); | ||
|
||
if ( false == hidden ) { | ||
newPostBtn.innerText = 'X'; | ||
} else { | ||
newPostBtn.innerText = 'New Post +'; | ||
} | ||
|
||
} | ||
|
||
function loadMessage( type = 'saved' ) { | ||
|
||
const message = document.createElement( 'div' ), | ||
savedMsg = 'This post has been saved!'; | ||
let markup = '<p>'; | ||
|
||
message.id = 'message'; | ||
|
||
if ( 'saved' == type ) { | ||
message.classList.add( 'saved' ) | ||
markup += savedMsg; | ||
} | ||
|
||
markup += '</p>'; | ||
message.innerHTML = markup; | ||
|
||
mainContainer.insertBefore( message, mainContainer.childNodes[ 0 ] ); | ||
|
||
setTimeout( function () { | ||
document.getElementById( "message" ).remove(); | ||
}, 3000 ); | ||
|
||
} | ||
|
||
function loadPost( post ) { | ||
|
||
const article = document.createElement( 'article' ); | ||
let markup = ''; | ||
|
||
markup += `<h2 class="entry-title"><a href="${post.link}">${post.title.rendered}</a></h2>`; | ||
markup += `<div class="entry-content">${post.content.rendered}</div>`; | ||
article.classList.add( 'post' ); | ||
article.innerHTML = markup; | ||
appContainer.append( article ); | ||
|
||
} | ||
|
||
function clearForm() { | ||
formTitle.value = ''; | ||
tinyMCE.activeEditor.setContent( '' ); | ||
} | ||
|
||
function clearPosts() { | ||
appContainer.innerHTML = ''; | ||
} |
64 changes: 64 additions & 0 deletions
64
5 - Using the WP REST API Inside WP/5.4-save-posts-completed/assets/js/theme.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
(function () { | ||
|
||
(function init() { | ||
|
||
setupListeners(); | ||
loadPosts(); | ||
|
||
})(); | ||
|
||
function setupListeners() { | ||
|
||
newPostBtn.addEventListener( 'click', togglePostForm ); | ||
|
||
// Change SAVE_POST_FUNCTION to savePost | ||
savePostBtn.addEventListener( 'click', savePost ); | ||
|
||
} | ||
|
||
function savePost( event ) { | ||
|
||
// Change TITLE_HERE to formTitle.value | ||
// Change CONTENT_HERE to tinyMCE.activeEditor.getContent() | ||
// Change STATUS_HERE to 'publish' | ||
const post = { | ||
'title': formTitle.value, | ||
'content': tinyMCE.activeEditor.getContent(), | ||
'status': 'publish' | ||
}; | ||
|
||
// Change POST_HERE to post | ||
let newPost = new wp.api.models.Post( post ); | ||
|
||
event.preventDefault(); | ||
togglePostForm(); | ||
|
||
// Change SAVE_POST() to newPost.save() | ||
newPost.save() | ||
.done( () => { | ||
loadMessage( 'saved' ); | ||
clearForm(); | ||
loadPosts(); | ||
} ); | ||
|
||
} | ||
|
||
function loadPosts() { | ||
|
||
let posts = new wp.api.collections.Posts(); | ||
|
||
posts.fetch( { | ||
data: { | ||
per_page: 3, | ||
} | ||
} ) | ||
.done( () => { | ||
clearPosts(); | ||
posts.each( post => { | ||
loadPost( post.attributes ); | ||
} ); | ||
} ); | ||
|
||
} | ||
|
||
})(); |
49 changes: 49 additions & 0 deletions
49
5 - Using the WP REST API Inside WP/5.4-save-posts-completed/custom.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
// Template Name: Custom JS Page | ||
get_header(); | ||
?> | ||
|
||
<div id="primary" class="content-area"> | ||
|
||
<main id="main" class="site-main" role="main"> | ||
|
||
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> | ||
|
||
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> | ||
|
||
<header class="entry-header"> | ||
|
||
<?php the_title(); ?> | ||
|
||
</header> | ||
|
||
<div class="entry-content"> | ||
|
||
<?php if ( is_user_logged_in() && current_user_can( 'edit_others_posts' ) ): ?> | ||
|
||
<p><a class="button toggle-add-form" href="#add-new">New Post +</a></p> | ||
|
||
<div id="add-post-form" class="hidden"> | ||
<h3 class="add-new-post">Add New Post</h3> | ||
<h3><input id="new-title" class="title-editor" type="text" name="title" placeholder="Enter title here" value=""></h3> | ||
<?php wp_editor('', 'editor'); ?> | ||
<p><input id="save-post" class="button" type="submit" name="" value="Save"></p> | ||
</div> | ||
|
||
<?php endif; ?> | ||
|
||
<div id="list-posts"></div> | ||
|
||
</div> | ||
|
||
</article> | ||
|
||
<?php endwhile; endif; ?> | ||
|
||
</main> | ||
|
||
</div> | ||
|
||
<?php get_sidebar(); ?> | ||
|
||
<?php get_footer(); ?> |
17 changes: 17 additions & 0 deletions
17
5 - Using the WP REST API Inside WP/5.4-save-posts-completed/footer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
</div><!-- #content --> | ||
|
||
<footer id="colophon" class="site-footer" role="contentinfo"> | ||
|
||
<a href="<?php echo esc_url( __( 'https://wordpress.org/', 'jsforwp' ) ); ?>"> | ||
<?php printf( esc_html__( 'Proudly powered by %s', 'jsforwp' ), 'WordPress' ); ?> | ||
</a> | ||
|
||
</footer> | ||
|
||
</div><!-- #page --> | ||
|
||
<?php wp_footer(); ?> | ||
|
||
</body> | ||
</html> |
79 changes: 79 additions & 0 deletions
79
5 - Using the WP REST API Inside WP/5.4-save-posts-completed/functions.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
// Add Theme Support | ||
add_theme_support( 'title-tag' ); | ||
add_theme_support( 'post-thumbnails' ); | ||
add_theme_support( 'html5' ); | ||
add_theme_support( 'automatic-feed-links' ); | ||
|
||
|
||
// Load in our JS | ||
function jsforwp_enqueue_scripts() { | ||
|
||
if( is_page_template( 'custom.php' ) ) { | ||
|
||
wp_enqueue_script( | ||
'jsforwp-helpers-js', | ||
get_stylesheet_directory_uri() . '/assets/js/helpers.js', | ||
[], | ||
time(), | ||
true | ||
); | ||
wp_enqueue_script( | ||
'jsforwp-theme-js', | ||
get_stylesheet_directory_uri() . '/assets/js/theme.js', | ||
[ 'wp-api', 'jsforwp-helpers-js' ], | ||
time(), | ||
true | ||
); | ||
|
||
} | ||
|
||
} | ||
add_action( 'wp_enqueue_scripts', 'jsforwp_enqueue_scripts' ); | ||
|
||
// Load in our CSS | ||
function jsforwp_enqueue_styles() { | ||
|
||
wp_enqueue_style( 'roboto-slab-font-css', 'https://fonts.googleapis.com/css?family=Roboto+Slab', [], '', 'all' ); | ||
wp_enqueue_style( 'main-css', get_stylesheet_directory_uri() . '/style.css', ['roboto-slab-font-css'], time(), 'all' ); | ||
wp_enqueue_style( 'custom-css', get_stylesheet_directory_uri() . '/assets/css/custom.css', [ 'main-css' ], time(), 'all' ); | ||
|
||
} | ||
add_action( 'wp_enqueue_scripts', 'jsforwp_enqueue_styles' ); | ||
|
||
// Control header for the_title | ||
function jsforwp_title_markup( $title, $id = null ) { | ||
|
||
if ( !is_singular() && in_the_loop() ) { | ||
$title = '<h2><a href="' . get_permalink( $id ) . '">' . $title . '</a></h2>'; | ||
} else if ( is_singular() && in_the_loop() ) { | ||
$title = '<h1>' . $title . '</h1>'; | ||
} | ||
return $title; | ||
} | ||
add_filter( 'the_title', 'jsforwp_title_markup', 10, 2 ); | ||
|
||
|
||
// Register Menu Locations | ||
register_nav_menus( [ | ||
'main-menu' => esc_html__( 'Main Menu', 'wpheirarchy' ), | ||
]); | ||
|
||
|
||
// Setup Widget Areas | ||
function jsforwp_widgets_init() { | ||
register_sidebar([ | ||
'name' => esc_html__( 'Main Sidebar', 'jsforwp' ), | ||
'id' => 'main-sidebar', | ||
'description' => esc_html__( 'Add widgets for main sidebar here', 'jsforwp' ), | ||
'before_widget' => '<section class="widget">', | ||
'after_widget' => '</section>', | ||
'before_title' => '<h2 class="widget-title">', | ||
'after_title' => '</h2>', | ||
]); | ||
} | ||
add_action( 'widgets_init', 'jsforwp_widgets_init' ); | ||
|
||
|
||
?> |
39 changes: 39 additions & 0 deletions
39
5 - Using the WP REST API Inside WP/5.4-save-posts-completed/header.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<link rel="profile" href="http://gmpg.org/xfn/11"> | ||
<?php wp_head(); ?> | ||
</head> | ||
<body <?php body_class(); ?>> | ||
|
||
<div id="page"> | ||
|
||
<a href="#content" class="skip-link screen-reader-text"> | ||
<?php esc_html_e( 'Skip to content', 'jsforwp' ); ?> | ||
</a> | ||
|
||
<header id="masthead" class="site-header" role="banner"> | ||
|
||
<div class="site-branding"> | ||
<p class="site-title"> | ||
<a href="<?php echo esc_url( home_url() ) ;?>" rel="home"> | ||
<?php bloginfo( 'name' ); ?> | ||
</a> | ||
</p> | ||
<p class="site-description" > | ||
<?php bloginfo( 'description' ); ?> | ||
</p> | ||
</div> | ||
|
||
<nav id="site-navigation" class="main-navigation" role="navigation"> | ||
<?php | ||
$args = [ 'theme_location' => 'main-menu' ]; | ||
wp_nav_menu( $args ); | ||
?> | ||
</nav> | ||
|
||
</header> | ||
|
||
<div id="content" class="site-content"> |
33 changes: 33 additions & 0 deletions
33
5 - Using the WP REST API Inside WP/5.4-save-posts-completed/index.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php get_header(); ?> | ||
|
||
<div id="primary" class="content-area"> | ||
|
||
<main id="main" class="site-main" role="main"> | ||
|
||
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> | ||
|
||
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> | ||
|
||
<header class="entry-header"> | ||
|
||
<?php the_title(); ?> | ||
|
||
</header> | ||
|
||
<div class="entry-content"> | ||
|
||
<?php the_content(); ?> | ||
|
||
</div> | ||
|
||
</article> | ||
|
||
<?php endwhile; endif; ?> | ||
|
||
</main> | ||
|
||
</div> | ||
|
||
<?php get_sidebar(); ?> | ||
|
||
<?php get_footer(); ?> |
18 changes: 18 additions & 0 deletions
18
5 - Using the WP REST API Inside WP/5.4-save-posts-completed/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# 5.4 - Saving Posts | ||
|
||
1. Open '/assets/js/theme.js' (also note there is a helpers.js file as well) | ||
2. Inside of setupListeners() find the event listener attached to savePostBtn | ||
3. Change SAVE_POST_FUNCTION to savePost | ||
4. Find the savePost() function | ||
5. Change TITLE_HERE to formTitle.value | ||
6. Change CONTENT_HERE to tinyMCE.activeEditor.getContent() | ||
7. Change STATUS_HERE to 'publish' | ||
8. Find 'let newPost = ..' and change POST_HERE to post | ||
9. Find the SAVE_POST() function and change to to newPost.save() | ||
|
||
|
||
### Testing | ||
10. Login to your site and assign "Custom JS Page" to one of your pages | ||
11. View that page in the browser | ||
12. Try adding new posts. You should see message that post is saved and list of posts updated. | ||
|
11 changes: 11 additions & 0 deletions
11
5 - Using the WP REST API Inside WP/5.4-save-posts-completed/sidebar.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
if( ! is_active_sidebar( 'main-sidebar' ) ) { | ||
return; | ||
} | ||
?> | ||
|
||
<aside id="secondary" class="widget-area" role="complementary"> | ||
|
||
<?php dynamic_sidebar( 'main-sidebar' ); ?> | ||
|
||
</aside> |
Oops, something went wrong.