Create new admin section in wordpress - for creating office-pages - php

Ok, I have been looking for hours and I have to say I am really lost. I am trying to create a new section in the admin section of wordpress that should enable the user to create a new custom "office" page.
There is really many office pages on the site I have been working on (over 30), each with its opening hours, map, and images. I assume the client will want to add more later (or remove them) and they would like to manage it through Wordpress. That would mean adding a section that would enable them to put in the name of the office, opening hours, images and the location and it would create a new office page. I am rather a front-end developer and I have never worked with Wordpress before. I understand the loop, etc, I have read several things about Themes and how to create them but I am seriously stuck with how to create a section in admin area that would enable page creation/deletion with certain options.
Any help is greatly appreciated, just please point me to the correct direction. Web pages, WP codex, tutorials, youtube... whatever that helps. Thanks a bunch!

You can make custom post type..
By writing Code in function.php
function function-name(){
register_post_type( 'post name',
array(
'labels' => array(
'name' => __( 'post name' ),
'singular_name' => __( 'post name' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'post name'),
'supports' => array('title','editor','author','thumbnail','comments','custom-fields'),
)
);
}
add_action('init', 'function-name');

3 steps : -
1. create office post type by using Register Post Type
2. Create office categories by using Register taxonomy
3. create a meta box by using Add Meta Box for extra fields which wordpress doesnt offer by default like (office hours )
hope it helps !

Related

Wordpress redirects single post permalink to the front-page.php file

Having a strange wordpress custom theme issue that I can't crack.
I have a custom post type 'artists' that I am displaying posts for that include a link with the_permalink();
I expect this permalink to take me to the template that I have named single-artists.php -- but instead it takes me to front-page.php every time, even though the link in the browser is the correct permalink that should take me to the single post page.
I have refreshed my permalinks in settings several times, and I have loaded the page and flushed my cache/removed cookies. Still going to front-page.php, no matter what post I click on, even though the permalink does change each time.
Anything else I should be doing? AFAIK the naming convention for single-artists.php is correct as the post type IS artists.
Classic case of me being a beginner, I hadn't noticed that my posts had been hidden from public. Setting public to true (like below) fixed it. If there's any other newbies who run into this hopefully this helps you!
function create_artist_post() {
$args = array(
'labels' => array(
'name' => 'Artists',
'singular_name' => 'Artist'
),
'public' => true,
'show_ui' => true,
'menu-position' => 20
);
register_post_type('artists', $args);
}
add_action('init', 'create_artist_post');

CPT remove single view Wordpress frontend

I added a custom post type 'Catalog'.
This CPT makes it posible to create A list of items like a catalog.
For posts Wordpress has always a single view page on the frontend and I want remove these and show an 404 error if somebody tried visit the url.
register_post_type( 'catalog',
array(
'labels' => array(
'name' => 'Catalogus',
'add_new' => 'Nieuw item',
'add_new_item' => 'Nieuw item toevoegen',
'new_item' => 'Nieuw item',
),
'public' => false,
'show_in_rest' => false,
'menu_icon' => 'dashicons-store',
'menu_position' => 2
)
);
I set the public on false but this removes the whole CPT on the front and adminside.
Who can help me?
When you register a custom post type, a number of other values will set their defaults based on the value used for public. By setting public to false, the argument to show the UI in the admin panel will also have defaulted to false.
Pass in show_ui to control whether it's displayed in the admin panel:
register_post_type( 'catalog', [
'labels' => [
// labels...
]
'public' => false,
'show_in_rest' => false, // default is false so probably not needed
'show_ui' => true, // show the admin UI for the CPT even when public is false
'menu_icon' => 'dashicons-store',
'menu_position' => 2
] );
https://developer.wordpress.org/reference/functions/register_post_type/#public
For anyone in the community that's using the Custom Post Type's UI (CPT UI) plugin, just set "Publicly Queryable" to false. This will disable the single page for your CPT.
Any (custom) post types is ruled by the Wordpress Template Hierarchy.
As you can see here with the Visual Overview, any post type will be displayed by the following templates:
Custom Post Type
$custom.php → (fallback) single-$posttype-$slug.php → (fallback) single-$posttype.php → (fallback) single.php → (fallback) singular.php
Blog Post
$custom.php → (fallback) single.php → (fallback) singular.php
One way to remove the single custom post type display ability and keeping the query ability is to remove and add the adequate templates.
Following best practices, by removing single.php & singular.php and not including any custom templates, you will effectively remove a custom post type ability to produce a single template which will redirect the request to a 404.php and corresponding fallbacks.
To make sure our blog posts still renders and have a single post type display ability, we need to include a single-post.php which will only display theme's blog posts.
Here is a minimal graphical representation of a theme's folder with no single custom post type ability and a single blog post post ability.
myTheme/
├── style.css/
├── single-post.php/
└── index.php/

how to create custom post type with templates

I am working on a project that requires a custom post type.
I am also required to have a breadcrumb that works with all types of post/page types.
The thing is that I haven't figured out a good way to achieve to have a custom post type that has a "startpage" and then some child pages.
in my world, i would like to set up a regular page (let's call it "cars") that I link to a template. The template should be somehow connected to the custom post type so when I create a post in custom post type (let's call it "BMW") and goes to that post, I want the website to be like http://test.com/cars/BMW.
The other problem here is that when I am on the page http://test.com/cars/bmw and what to run a function to see what my page ancestors the BMW page has, I get null because Wordpress recognise the BMW page as a post and cars as a page.
I don't know if you guys understand what I mean here but what is your best way to achieve a complete chain with custom post type so I can access or recognize all pages/post in the chain. Also, this is mostly for the breadcrumbs to work and I want that standard first-page "cars" in this chain so one can write content in the standard WYSIWYG that will appear on the site.
Thanks, everyone.
Updated
When you register the post type, you specify a slug which will be used for your URL base, provided has_archive is set to true. In this case you can use the archive-{slug}.php file to style the archive.
If you want to use a custom page instead, has_archive has to be false but then there's no reliable way to identify this "fake parent" from the single CPT post.
The best option is to introduce a configuration setting on a custom settings page the allows the user to select which page to use for the CPT's archive. In the breadcrumbs generation you'd then use this config value instead of hardcoding or URL string manipulation etc.
Your CPT:
register_post_type('cars', [
'labels' => [
'name' => 'Cars',
'singular_name' => 'Car'
],
'supports' => ['title', 'editor', 'author', 'thumbnail', 'comments', 'revisions'],
'taxonomies' => ['category', 'post_tag'],
'public' => true,
'exclude_from_search' => false,
'has_archive' => false,
'rewrite' => ['slug' => 'cars']
]);
After you change settings that affect rewrite rules, go to Dashboard > Settings > Permalinks page and click save to rebuild the cache.

Add field to wordpress billing fields

I'd like to add 2 custom fields to the user billing information in WooCommerce, one for capturing their VAT number and one for the Chamber of Commerce number. These fields need to be displayed during checkout, on the my account > billing address page and also on the WP Admin on the user page so the admin of the website/webshop can check for these values.
I prefer not to use a plugin but to use the child theme's functions.php.
Can anyone please help me with this problem? I looked around on the Wordpress Stack exchange but couldn't find a specific and up-to-date solution for my problem. Also I read the Woocommerce documentation but there it's not explained how to show the custom billing fields on the user admin page in the backend.
Thank you very much in advance!
You can achieve this by using Woocommerce filter woocommerce_checkout_fields.
Here is the code example.
add_filter( 'woocommerce_checkout_fields','checkout_extra_fields');
function checkout_extra_fields($fields){
$fields['billing']['vat_number'] = array(
'label' => __('VAT number', 'my-slug'),
'placeholder' => __('VAT number', 'my-slug'),
'required' => false,
'clear' => false,
'type' => 'text',
);
$fields['billing']['commerce_number'] = array(
'label' => __('Commerce number', 'my-slug'),
'placeholder' => __('Commerce number', 'my-slug'),
'required' => false,
'clear' => false,
'type' => 'text'
);
return $fields;
}
Also, You can get the saved values from backend by this code
$extra_fileds_vat_number = get_post_meta( wf_get_order_id($order),'_vat_number',1);
$extra_fileds_commerce_number = get_post_meta( wf_get_order_id($order),'_commerce_number',1);
Altough I didn't want to use a plugin and Nishad's answer is absolutely correct I found a free plugin (after a really long search) that does exactly what I want. It's called Flexible Checkout Fields and you can create custom fields that show up on Checkout, Woocommerce my account page and on the WP admin user profile page. I really love it!
So for anyone who encounters the same problem, you might want to try this one!

Altering WordPress Link Structure for Portfolio

Is it possible / easy to change the permalink format for portfolios?
I want to have a page called projects, and each “portfolio’ item as a project.
Site
So /projects
and each project is under that: /projects/example/
instead of /projects -> /portfolio/example/
It is possible, but I think that the author of the theme that named this custom post type as 'portfolio' can alter it in the code of the theme,, . It can be hacky to change it on your own because you may break things..
You can try to ask the developers of the theme that you bought to help you with that, I belive it is this theme
http://centum.purethe.me/
right?
Hope they can help you with that, and if not, I'm afraid if you do not post some specific code than community can't help you much either,,,
hth, k
You should check the files of the theme and look for the file where the portfolio posttype is added. You could add here the following code to rewrite the slug.
$rewrite = array(
'slug' => 'project',
'with_front' => true,
'pages' => true,
'feeds' => true,
);
Most of the time the file where the posttypes are added are in the 'lib' folder or something like that.

Categories