Customize WordPress Editor - php

Sorry if my terminology is confusing but I am unsure of what call this exactly.
In Wordpress when editing a post or page type, on the right side of the editor there is the sections Publish, Attributes, Featured Image, etc. I'm wondering if there is a way where I can add my own section to the right side like the other? Is it custom post type or something more complicated?
I'm trying to stay within the possiblity of using hooks.

Those are 'meta boxes'
http://codex.wordpress.org/Function_Reference/add_meta_box
function my_custom_meta_boxes()
{
add_meta_box(
'custom1', // id
'Custom Metabox', // title
'display_custom_meta_box', // callback for display
'post', // post type
'normal', // position
'high' // priority
);
}
add_action('add_meta_boxes', 'my_custom_meta_boxes');
function display_custom_meta_box()
{
echo 'Your meta box here';
}
There are plugins available to create meta boxes quickly e.g.
http://wordpress.org/extend/plugins/meta-box/
http://wordpress.org/extend/plugins/types/

http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates
Read up about it here^
Wasn't that hard to google! :)

Related

WordPress Custom Fields w/ Shortcodes to use in Posts and Pages

I am new to WordPress but learning very fast. I started my first plugin that creates a custom admin menu. One of the submenus is named Business Profile. Here is what I want to do:
1). Add custom fields to the Business Profile to collection business info (ex: Business Name)
2). Each custom field needs a related shortcode
3). All shortcodes can be placed within content on Posts and Pages and replaced with the custom field value.
I'm looking for a tutorial or some direction on how to do the above without the use of a plugin. Can anyone point me in the right direction?
I believe that you want to store and retrieve only one business information.
function sample_register_my_custom_menu_page(){
add_menu_page(
__( 'Business Profile', 'business-profile' ),
'Business Profile',
'manage_options',
'business-profile-settings',
'business_profile_settings_page',
80
);
}
add_action( 'admin_menu', 'sample_register_my_custom_menu_page' );
function business_profile_settings_page(){
if ($_POST) {
// Form Submission Processing
$business_profile = array();
$business_profile['business_name'] = sanitize_text_field($_POST['business_name']);
update_option('business_profile_details', $business_profile);
}
// Form View
// Add your html form here
$business_profile = get_option( 'business_profile_details' );
// Get your saved data and pre fill your form.
}
You can further use add_shortcode to get the data from the options and display it the post.
Note: Use wp_options to store this data only if this is a single data set. If you want to save data of multiple business you may need to use a custom post for it.

switch_to_blog() within WordPress shortcode

Using a WordPress multisite network, I need to access custom post types from our main site from within a shortcode.
For example, our main site (ID 1) is where the custom post types (Case Studies) are stored. In functions.php I have the following:
//[casestudy]
add_shortcode( 'casestudy', 'casestudy_shortcode' );
function casestudy_shortcode( $atts ) {
$a = shortcode_atts( array(
'id' => ''
), $atts );
switch_to_blog(1);
//Get fields from custom post type with Advanced Custom Fields Pro
//and return HTML output with them
restore_current_blog();
}
Then call the shortcode with [casestudy id="123"] where the ID is the post ID of the case study.
The problem is, doing this returns the Case Study HTML fine but breaks some features of the page and also populates the 'recent posts' widget with blog posts of the main site.
Any ideas on what's going wrong? Thanks.
Adding my comment as an answer:
Reading the OP code comments, it looks like restore_current_blog() is never called, because the HTML is returned before calling it.
Make sure that the return part is the last line in there.

Yoast SEO | How to create custom variables

Was just wondering if there is a way to create a custom variable so that I can add my custom variable created in the Meta title of the page.
Yoast SEO has a list of variables predefined here.
It would be great if I could create a variable of my own. Is there any way to get this?
Thanks in advance!
You have two options for this.
Add filter for change exist variable.
Add your new custom variable.
If you want to change exist variable, you can do it like this:
// define the wpseo_replacements callback
function filter_wpseo_replacements( $replacements ) {
if( isset( $replacements['%%page%%'] ) ){
$replacements['%%page%%'] = 'Page x of y';
}
return $replacements;
};
// Add filter
add_filter( 'wpseo_replacements', 'filter_wpseo_replacements', 10, 1 );
And if do you want to add custom variable you can do it like this:
// define the custom replacement callback
function get_myname() {
return 'My name is Moses';
}
// define the action for register yoast_variable replacments
function register_custom_yoast_variables() {
wpseo_register_var_replacement( '%%myname%%', 'get_myname', 'advanced', 'some help text' );
}
// Add action
add_action('wpseo_register_extra_replacements', 'register_custom_yoast_variables');
I hope I was helpful to you.
There is a way, but as far as I know, you must get a Premium account in Yoast Seo. One of the most important functions of both Yoast SEO and Yoast SEO Premium is the possibility to add title templates and meta description templates to the homepage, all (custom) post types, all (custom) taxonomies and other pages.
Note: Template variables can also be used on individual posts, pages and taxonomies.
Setting up / changing your templates
You can change your title & meta templates by going to the admin of your WordPress installation and clicking SEO → Titles & Metas.
Log in to your WordPress website. When you're logged in, you will be in your 'Dashboard'. On the left-hand side, you will see a menu. In that menu, click on 'SEO'.
The 'SEO' settings will expand providing you additional options. Click on 'Titles & Metas'.
Under each tab, you can use these variables to create templates for various pages within your site.
You can use the variables from the partial list below to create your own templates for the titles and meta-descriptions. The full list of variables is listed on the HELP tab of the plugin. Just go to SEO → Titles & Metas and click the help tab in the top right.
For more about that and see many images of the process, please visit this page: http://kb.yoast.com/article/146-yoast-wordpress-seo-titles-metas-template-variables

Custom field not showing in admin Wordpress

As I created custom post type with taxonomy, when I add a new custom field, it is saving in the database but not showing in custom fields. I don't understand how to show the custom field in admin panel.
There is a very good plugin called Advanced Custom Fields for Wordpress. It's very easy to use and features conditional logic for page layouts, post type and much more.
We can easily create a meta box without using any plugin and customize it as per our needs:
here is the Wordpress documentation to create a meta box and here is the example to easily implement it with custom post type. Here is the example:
<?php
add_action('add_meta_boxes', 'meta_box_add_function');;
function meta_box_add_function()
{
add_meta_box(
'wporg_box_id', // Unique ID
'Custom Meta Box Title', // Box title
'wporg_custom_box_html', // Content callback, must be of type callable
'post' // Post type ['post', 'custom_post_type']
);
// You can add multiple boxes like above
}
function wporg_custom_box_html($post){
echo 'What you put here, show\'s up in the meta box';
}
?>
And here you can save the post data using below hook:
<?php add_action( 'save_post', 'meta_box_save_function' ); ?>

Add "custom page" without page

The title might not be completely clear, but I didn't know how to ask this in another way.
I want to build a system in Wordpress where the user can put some projects together where it would be on an url like http://mywordpress.com/projectbuilder/ or something like that.
Normally I would create an page in the admin menu and set it to a certain template and in the content I would put some text like: "Do not delete this page, this content is not shown".
But I think there must be a better way to add a custom page to a certain URL without adding it in the backend as a page with "useless content" since the content would not be changeable from the backend in this case.
I hope this makes sense. How could I go about that?
I think I could achieve this with a custom plugin but I can't seem to find any code how to go about that. I have found a way to add administration pages in the settings menu on the right. But I want to add a page to the website on the front end.
Sorry i didn't got your question properly. but some what say to create Custom post or taxonomy :
Please check below link
Custom Post and Taxonomies
In your functions.php file add this anywhere:
function themeslug_projects() {
$args = array(
'public' => true,
'label' => 'Projects',
'rewrite' => array( 'slug' => 'projects' ),
);
register_post_type( 'projects', $args );
}
add_action( 'init', 'themeslug_projects' );
Do save your permalink settings again after doing this, this will work surely then..
basically you can do this by creating a rewrite rule and then point to a file.
add_action('init', 'add_rewrite_rule');
function add_rewrite_rule(){
// add_rewrite_rule(REGEX url, location, priority (i.e. top is before other rewrite rules)
//basically tell wordress to add a query var if sidebar is added to url. change sidebar to what you want your link to be.
// i set up a custom post type to make this work called custompostype..it does nothing but just to give post_type a value.
add_rewrite_rule('^sidebar?','index.php?is_sidebar_page=1&post_type=customposttype','top');
}
// register a query var
add_action('query_vars','market_set_query_var');
function market_set_query_var($vars) {
array_push($vars, 'is_sidebar_page');
return $vars;
}
// associate a template with your quer_var
add_filter('template_include', 'market_include_template', 1000, 1);
function market_include_template($template){
if(get_query_var('is_sidebar_page')){
$new_template = (theme or plugin path).'/pages/yourpage.php'; // change this path to your file
if(file_exists($new_template))
$template = $new_template;
}
return $template;
}
after adding any rewrite rule, the changes wont take place until you go into settings->permalinks and hit the "save" button.

Categories