Skip to content

Instantly share code, notes, and snippets.

@joshdcomp
Created March 16, 2017 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshdcomp/0d913c8b8d568db8aa602904100f3d6a to your computer and use it in GitHub Desktop.
Save joshdcomp/0d913c8b8d568db8aa602904100f3d6a to your computer and use it in GitHub Desktop.
Set up an options page with text fields for fb, twitter, & github
<?php
/**
* Sets up theme-wide options
*
* @since _geekery_ 1.0
*/
use Carbon_Fields\Container;
use Carbon_Fields\Field;
Container::make('theme_options', 'Geekery settings')
->set_page_permissions('administrator')
->add_fields([
Field::make("separator", "_geekery_social_handles_separator", "Social handles"),
Field::make('text', 'crb_geekery_social_handle_facebook', 'Facebook'),
Field::make('text', 'crb_geekery_social_handle_twitter', 'Twitter'),
Field::make('text', 'crb_geekery_social_handle_github', 'Github'),
Field::make("separator", "_geekery_homepage_jumbotron_image_separator", "Homepage Jumbotron"),
Field::make('image', 'crb_geekery_homepage_jumbotron_image', 'Select image'),
]);
if (function_exists('carbon_get_theme_option')) {
/*
* Adds endpoint for the homepage jumbotron
*
* @since _geekery_ 1.0
*/
function wp_api_get_home_jumbotron( WP_REST_Request $request ) {
$_jumbotron = carbon_get_theme_option('crb_geekery_homepage_jumbotron_image');
$jumbotron = wp_get_attachment_image_src($_jumbotron, 'full')[0];
return ['jumbotron' => $jumbotron];
}
add_action('rest_api_init', function () {
// full path will be /wp-json/geekery/v1/homepage/jumbotron
register_rest_route( 'geekery/v1', '/homepage/jumbotron', [
'methods' => 'GET',
'callback' => 'wp_api_get_home_jumbotron',
]);
});
/*
* Adds endpoint for social profile links
*
* @since _geekery_ 1.0
*/
function wp_api_get_social_links( WP_REST_Request $request ) {
//facebook
$fbHandle = carbon_get_theme_option('crb_geekery_social_handle_facebook');
//twitter
$twHandle = carbon_get_theme_option('crb_geekery_social_handle_twitter');
//github
$ghHandle = carbon_get_theme_option('crb_geekery_social_handle_github');
return [
'facebook' => ($fbHandle ? $fbHandle : null),
'twitter' => ($twHandle ? $twHandle : null),
'github' => ($ghHandle ? $ghHandle : null)
];
}
add_action('rest_api_init', function () {
// full path will be /wp-json/geekery/v1/social-links
register_rest_route( 'geekery/v1', '/social-links', [
'methods' => 'GET',
'callback' => 'wp_api_get_social_links',
]);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment