Skip to content

Instantly share code, notes, and snippets.

Pinal pinalj

Block or report user

Report or block pinalj

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@pinalj
pinalj / my-posts-filter.php
Last active Dec 1, 2017
Filtering Posts based on Author ID - REST API
View my-posts-filter.php
<?php
add_action( 'rest_api_init', 'my_register_route');
function my_register_route() {
register_rest_route( 'my-route', 'my-posts/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_posts',
'args' => array(
'id' => array(
'validate_callback' => function( $param, $request, $key ) {
@pinalj
pinalj / my-posts.php
Created Dec 1, 2017
Display all posts using REST API
View my-posts.php
<?php
add_action( 'rest_api_init', 'my_register_route');
function my_register_route() {
register_rest_route( 'my-route', 'my-posts', array(
'methods' => 'GET',
'callback' => 'my_posts',
'permission_callback' => function() {
return current_user_can( 'edit_others_posts' );
@pinalj
pinalj / permission_check.php
Last active Dec 1, 2017
Check for permissions for API
View permission_check.php
<?php
add_action( 'rest_api_init', 'my_register_route' );
function my_register_route() {
register_rest_route( 'my-route', 'my-phrase', array(
'methods' => 'GET',
'callback' => 'custom_phrase',
'permission_callback' => function() {
return current_user_can( 'edit_posts' );
},
@pinalj
pinalj / hello_world.php
Last active Nov 30, 2017
Create custom REST route
View hello_world.php
<?php
add_action( 'rest_api_init', 'my_register_route' );
function my_register_route() {
register_rest_route( 'my-route', 'my-phrase', array(
'methods' => 'GET',
'callback' => 'custom_phrase',
)
);
}
@pinalj
pinalj / multiple-notices.php
Last active Nov 3, 2017
Adding multiple Notices
View multiple-notices.php
<?php
add_action( 'admin_notices', 'my_info_notice' );
function my_info_notice() {
?>
<div class="notice notice-info">
<p><?php _e( 'You have successfully completed the setup.', 'my-text-domain' ); ?></p>
</div>
<?php
}
@pinalj
pinalj / update-option.php
Last active Nov 3, 2017
Update the option record
View update-option.php
<?php
add_action( 'wp_ajax_my_dismiss_notice', 'my_dismiss_notice' );
function my_dismiss_notice() {
update_option( 'my_dismiss_notice', true );
}
?>
@pinalj
pinalj / update-notice.js
Created Nov 2, 2017
JS file to run the AJAX
View update-notice.js
jQuery( document ).ready( function() {
jQuery( document ).on( 'click', '.my-dismiss-notice .notice-dismiss', function() {
var data = {
action: 'my_dismiss_notice',
};
jQuery.post( notice_params.ajaxurl, data, function() {
});
})
@pinalj
pinalj / include-js.php
Last active Nov 3, 2017
Include JS file
View include-js.php
<?php
add_action( 'admin_enqueue_scripts', 'add_script' );
function add_script() {
wp_register_script( 'notice-update', plugins_url() . '/add-notices/js/update-notice.js','','1.0', false );
wp_localize_script( 'notice-update', 'notice_params', array(
ajaxurl => get_admin_url() . 'admin-ajax.php',
));
@pinalj
pinalj / dismissible.php
Last active Nov 3, 2017
Adding a dismissible notice
View dismissible.php
<?php
if( get_option( 'my_dismiss_notice' ) != true ) {
add_action( 'admin_notices', 'add_dismissible' );
}
function add_dismissible() {
?>
<div class='notice notice-error my-dismiss-notice is-dismissible'>
<p>This is a dismissble notice.</p>
</div>
@pinalj
pinalj / conditional-notice.php
Last active Nov 3, 2017
Conditional Notice
View conditional-notice.php
<?php
if( ! empty( get_option( 'my_update_status' ) ) ) {
add_action( 'admin_notices', 'my_update_notice' );
}
function my_update_notice() {
?>
<div class="notice notice-success">
<p><?php _e( 'The update was completed successfully!', 'my-text-domain' ); ?></p>
</div>
You can’t perform that action at this time.