Wordpress admin: set category from variable instead of checkbox - wordpress

The only way to set a category to a new post seems to be to select the appropriate one via a check-box before submitting it.
At best, you can set a default category as an option in case no box has been checked by the user.
This is really a problem for me as my admin menus are and have to be the categories themselves.
As I mentioned, in my WordPress admin I have a custom menu listing category names. Clicking on one leads directly to the generic "add a new post" page.
What makes it less generic is that I have inserted the category ID within this link, like this: "wp-admin/post-new.php?cat=5".
At this point,I would like not to check a category from a box before submitting the new post. Instead, I want WordPress to use the variable provided in the URL and submit the category ID accordingly when my new post is ready to be published.
Is it possible?
Which file would I have to edit in order to achieve this?
Any other idea that would lead to the same result?

You can use an extension that you will develop. It is better than trying to modify Wordpress core files.
I did something that may help you in one of mine. Using this :
if(is_admin()){
add_action('post_submitbox_misc_actions', 'plugin_add_custom_box');
}
function plugin_add_custom_box(){
?>
<div class="al2fb_post_submit">
<div class="misc-pub-section">
<?php
echo '<input type="checkbox" id="plugin" name="plugin_action" value="1"/>';
echo '<label for="plugin">';
_e("Label", 'myplugin_textdomain');
echo '</label> ';
?>
</div>
</div>
<?php
}
This add a custom checkbox. Using the same name for the checkbox here than checkboxes already displayed by Wordpress and checking it analysing your URL, you will probably be able to store the category you want without modifying core files.

Related

Can we replicate ACF functionality in our theme?

Hi i want to add advance custom fields in my wordpress theme. I don't need editor functionality but to provide the users the facility to enter the values to view in the frontend.
basically i want to add the custom fields in my theme just like ACF but i don't want to use the plugin. Is there anything anyone can help me out with this please do.
I'm using ACF plugin right now to add custom fields in my theme.
For example I'm getting the designation from user in the admin panel and our team members custom post type. and showing it on the home page by using this code.
<p><span class="fa fa-user-circle"></span> Designation</strong>:<?php echo the_field( 'designation' ); ?> </p>
I don't want to use ACF plugin to perform this task. I know about the wordpress custom field. The problem with wordpress custom field is I've to select the key every time when I create a post. Here's a sample of what I'm trying to do. I want to add the this in my add new post.
Right now I've to select the key value whenever a new post is created. I want something similar to the image attached in my add new post. Thanks.
You can use native WordPress function(s) to do the same thing. Of course, you lose the extensive ACF features and slick ACF admin panels.
Instead of using ACF's get_field(), you can use get_post_meta(get_the_ID(), 'field', true);
List all items in the meta field 'appartments' like so:
$meta_name = 'appartments';
$fields = get_post_meta( $post->ID, $meta_name, true );
foreach ( $fields as $fieldValue )
{
echo $fieldValue . ' ';
}

WordPress Page Templates for Custom Post Type

I have a custom post type, "Store Pages" for instance, which is an almost identical duplicate of the default Wordpress "Page" post type.
Like the "page" post type, I would like to create page templates (not post-type templates) and be able to select them from the "Template" drop-down within the "Page attributes" box in the page editor.
I have created several templates, but the drop-down menu does not appear; I am assuming this is because a custom post types does not allow support for this.
Is there a way I can create page templates for a custom post type without using "single-{post-type-name}.php" and having a dozen queries to load up different template files?
I have double checked the comments for the templates are correct as they appear when I create a new page (post type, "Page").
Help would be greatly appreciated.
starting 4.7 you can use the new feature described here https://make.wordpress.org/core/2016/11/03/post-type-templates-in-4-7/
<?php
/*
Template Name: Full-width layout
Template Post Type: post, page, product
*/
// … your code here
If I understood correctly you want a Select template dropdown for your custom post type.
You can do this easily through Advanced Custom Fields, here's a quick guide how to get through.
After installing the plugin you can access the Custom Field section, if you open it and click Add new it bring you to the field editor. Name it whatever you want, it's just for administrative display.
On Field type choose "Select", this will allow you to construct a select box on your backend, keep in mind the value of "Field name" you will need this later on the code.
Scrolling down you can add the values of the select box in the following format: "key value : Textual label" just assume for now you want 2 templates, one for audio posts and one for video posts.
If you keep scrolling down you can see the display rule for this field group, now you will have "post" and "page" by default, when you add different content types you will have the additional content types here to select, just go ahead and pick yours.
And, ta-da. If you go on the custom content type edit window you will find your new fresh select box here waiting for you.
The code integration now is very simple, just go onto your single-{post-type-name}.php template and pull the custom field data into your loop. Then you can use this to use get_template_part() to pull your custom templates.
<?php $template_type = get_field('template'); // This must match with the field name value ?>
<?php if (isset($template_type) && !empty($template_type)): ?>
<?php get_template_part( 'store', $template_type ); ?>
<?php else: ?>
// You should have a fallback for the all the existing posts without template set or if you create a new post without a template.
<?php endif; ?>
In this specific example the pulled template files will be in the format of store-{key-value-of-the-selectbox}.php, of course you can readapt that for you best convenience.

WordPress MetaSlider & Advanced Custom Fields integration

I'm building a website in WordPress using the Advanced Custom Fields plugin alongside metaslider, I want the user to be able to select a metaslider from a dropdown list by using a custom field.
I know that you can add a slider in via the WYSIWYG however I want to make a seamless way of doing this, it's probably not as simple as I'd like it to be.
The website consists of projects and will have several sliders all will be appropriately named and I want to make it foolproof for the user.
I've searched online and can't seem to find anyone else who has wanted/tried to to this.
Ask an questions you have if i haven't been clear!
Any help would be greatly appreciated.
It's okay, I figured out how to do it!
You can select a field type of "post object" then add a "post type" of Meta Slider then the user has a dropdown box where they can select which slider they want to display on the page.
Metaslider shortcode id in ACF:
$slider_object['value']->ID
Try:
<?php $slider = get_field_object('slider_item'); ?>
<?php echo do_shortcode('[metaslider id="'.$slider['value']->ID.'"]'); ?>

Dropdown of existing posts in a metabox

I want to have ability to choose for each page what post should appear in a sidebar, from multiple posts type. So I understand that I need a meta box with a dropdown list of all posts, but I don't know how to build this in functions.
I only found this solution which is quite similar to what I want, but this doesn't help me to much, because I can only choose from a single post type and display only in post pages.
There is a free plugin that will solve all of your woes. It's called ACF or Advanced Custom Fields. It has the ability to add a list of posts to a field and attach that field to pages. Here's how you'd do it:
First install the plugin and navigate to the custom fields screen. Setup your field exactly like this:
Then in the options below that section you need to select these options:
That will tell ACF to put the field only on pages. After you have set that up you will get a little sidebar block like this:
You can then select each post for the page and it will return that object on the frontend. You do need to use a little code to get the frontend to spit out the posts you need. Here is the code to get a frontend option from ACF. Inside of the sidebar.php file you need to add this code:
global $post; // Get the global post object
$sidebar_posts = get_field('posts', $post->ID); // Get the field using the post ID
foreach($sidebar_posts as $sidebar_post){ // Loop through posts
echo $sidebar_post->post_title; // Echo the post title
}
This will simply loop through the posts you select and echo out the title. You can do more with this by adding some other Wordpress post functions using setup_postdata(). This will allow you to do things like the_title() and the_content().
Hope this helps!

How can I create a link to a specific page in wordpress?

It seems like a simple task, but I'm having difficult time finding any information on this. Basically I have an "Events" page created in wordpress to which I want to create a link on my main page. I do not want it to be static like (https://mysite.com/events). Is there a way to do it dynamically?
You want to use the get_permalink() function to write the link - http://codex.wordpress.org/Function_Reference/get_permalink
This function accepts an argument which is the post or page ID (and defaults to the ID of the current page), so if your Events page is ID #11, it would look like to following to also have the link text match the page title (should it change to "Events!" in the future, for example):
<?php $events_page = get_post(11); ?>
<?php echo $events_page->post_title; ?>

Resources