Plugin single.php not showing - wordpress

I'm creating a plugin for Wordpress that creates a new post type for news and events (yes, yet another one ;-) ). The post-type is registered under the name gg_nae.
The post type works as expected; the post type can be saved and edited.
Now I want to create a custom template file. I called it single-gg_nae.php and I saved it in the same folder as the plugin-code.
If I understand the explanation on https://developer.wordpress.org/plugins/post-types/working-with-custom-post-types/ correctly that should be enough to render the post type in the custom template, but it won't. It renders the post with the single.php file from the template.
If I move the custom template to the theme-folder however, Wordpress uses the template as intended; it renders the post in the single-gg_nae.php template.
What am I doing wrong here? Should single-gg_nae.php be placed in a specific folder in the plugin-map?
edit: I already saved the permalink-structure again, but that didn't help.

only for the missing in this three below parameter u are getting error
'rewrite' => array( 'slug' => 'slider' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => true,
try it in argument list with rest of parameters

I was thinking all wrong when approaching this issue.
I have created custom post types before and always included them in the functions.php of my Wordpress theme, therefor creating templates (archive-PostTypeName.php and single-PostTypeName.php) to show them. I assumed that was also needed when developing a CPT-plugin.
In case of a plugin, however, you have to alter the behavior of the single.php template from within the plugin using filters. I have added the following (simplified) code to my plugin:
/**
* Alter the_content if Posttype is custom post type
*
* #uses is_single()
*/
add_filter( 'the_content', 'my_the_content_filter', 20 );
function my_the_content_filter( $content ) {
if ( is_single() AND get_post_type() === 'PostTypeName') {
// Do stuff here
$content = 'This is My Custom Post Type!';
}
// Returns the content.
return $content;
}
When opening posts of the type 'PostTypeName' the content will say 'This is My Custom Post Type!', otherwise it'll show the content of the post of page.
More information about filters (and actions) can be in the Wordpress Codex

Related

Flutter - Retrieve Wordpress content that is not Post [duplicate]

I'm trying to get a list of taxonomies using the WordPress REST API. Hitting /wp-json/wp/v2/taxonomies/post_tag works fine, but I also have a custom taxonomy called location and accessing /wp-json/wp/v2/taxonomies/location returns a 403 rest_forbidden error.
I can't figure out under what circumstances taxonomy REST access would be forbidden in this way. Any ideas?
You need to set show_in_rest to true when registering your taxonomy.
https://codex.wordpress.org/Function_Reference/register_taxonomy
If your custom taxonomy was created by a plugin and you need to alter it's behaviour try this post :
http://scottbolinger.com/custom-taxonomies-in-the-wp-api/
In short, you can add the code below to your functions file to enable show_in_rest for all custom taxonomies.
function prefix_add_taxonimies_to_api() {
$args = array(
'public' => true,
'_builtin' => false
);
$taxonomies = get_taxonomies($args, 'objects');
foreach($taxonomies as $taxonomy) {
$taxonomy->show_in_rest = true;
}
}
add_action('init', 'prefix_add_taxonimies_to_api', 30);
I hope this helps you.

Custom-Field option not found in custom post type WP 4.6.1

I have created a custom post type call 'Movie'.Now,I want to add custom meta field in this call Movie Reviews.I have following code for custom post type.
function Create_Movies_Posttype()
{
register_post_type('Movies',
array(
'labels'=>array('name'=>__('Movies'),'singular_name'=>__('Movie')),
'public'=>true,
'has_archive'=>true,
'rewrite'=>array('slug'=>'movies'),
'support'=>array('title','custom-fields','edit'),
)
);
}
add_action('init','Create_Movies_Posttype');
Added Meta Box
function adding_custom_meta_boxes( $post ) {
add_meta_box(
'my-meta-box',
__( 'My Meta Box' ),
'render_my_meta_box',
'post',
'normal',
'default'
);
}
add_action( 'add_meta_boxes_post', 'adding_custom_meta_boxes' );
Any one please help me with this custom field.
You need to use add_meta_boxes() action to register metaboxes for a post type.
add_meta_box() function to define the box, and save_post action to save it.
You'll find everything you need by reading example on Codex add_meta_boxes
You need also to add supports parameter to the register post type arguments and add custom-field in the array defining the supports for it, all the native supports can be found here

Creating pages from Ninja form data

I've created a WordPress page with a Ninja form on it that collects miscellaneous data about a product, including some uploaded images. The page with the form is accessible from the main menu by clicking the "Input" item, so the user doesn't need to access the backend to upload their product data.
I now want to put this data into a custom post type called "Listing." There will eventually be thousands of these data sets and so thousands of "Listing" pages, as people come to the site, click Input in the main menu to get to the page with the Ninja form and fill it out.
Could someone tell me how they would go about now building these listing pages from the data the form has collected?
I'm running Ninja's Front-End Post option which supposedly will create a page from the form data. This plugin has some Post creation settings where you can select the post type to create, but this isn't working for me. I would expect the submitted form data to show up under dashboard | Listings, but there's nothing there after submitting the form.
Has anyone gotten this to work?
Thanks for your help.
I think you can use only Ninja Forms without extensions, and hook directly in 'ninja_forms_after_submission' that fires after submission and allow you to use data submitted and perform actions.
This is a starter codebase to achieve your result, but needs to be customized on your needs and your form structure.
add_action( 'ninja_forms_after_submission', 'create_page_from_ninjaform' );
function create_page_from_ninjaform( $form_data ){
// your fields data
$form_fields = $form_data[ 'fields' ];
// !!! this is an example, it depends form fields in your form
$title = $form_fields[ 1 ][ 'value' ];
$content = $form_fields[ 2 ][ 'value' ];
$sample_meta_field = $form_fields[ 3 ][ 'value' ];
$new_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_type' => 'listing', // be sure this is the post type name
);
$new_post_id = wp_insert_post( $new_post );
update_post_meta( $new_post_id, 'your_meta_key', $sample_meta_field );
}
This code should be copied in functions.php file
Not tested of course.
Good luck ;)
The Ninja Forms Front-end Posting extension isn't really meant for displaying form submission data on the front end.
From: https://ninjaforms.com/extensions/front-end-posting/
"The Ninja Forms Front-end Posting extension gives you the power of the WordPress post editor on any publicly viewable page you choose."
If you want to show Ninja Forms submission data on the front end, you will have to retrieve them from the database with code in functions.php or by writing a plugin (recommended). You could then parse and manipulate them and create a shortcode that would allow you to insert your formatted submission data easily in Wordpress posts or pages.
Here's a link to a feature request, asking for the same thing. The author of that request posted a link to a plugin (click Download as Plugin) they wrote which may do what you want or give you further insights as to how you could implement this.
https://github.com/wpninjas/ninja-forms/issues/892
If you do not mind paying a little money for a plugin I would recommend using gravity forms rather then ninja forms for more advanced stuff like this.
I manually create a custom post type "oproep" and used a gravityforms plugin to create a custom post from type oproep when an user submits the form.
Because you use custom post type archive pages www.mysite.com/oproep will be automatically created so you already have a list of "Listings". The singe pages www.mysite.com/oproep/title will also be created for you by default, you could override these templates as well if you would like depending on your theme.
The only thing you have to do is add a few php lines to your functions.php (or write your own plugin) that adds the custom post type. The rest all works automatically.
I went so far as writing code to make users able to edit their submissions, read custom taxonomy tags in dropdowns etc. You got lots and lots of more options using gravity forms.
FrancescoCarlucci's answer is correct, but just adding an additional comment: in case you want to specify by form field IDs which fields should go where in your post, NinjaForms passes the ID as a number (in my case for example, I needed field 136 for my post title). It may have been obvious but I racked my brain for a while until I figured it out.
function create_post($form_data) {
$form_fields = $form_data[ 'fields' ];
$post_fields = array(
'post_content' => '',
'post_content_filtered' => '',
'post_title' => '',
'post_excerpt' => '',
'post_status' => 'pending',
'post_type' => 'post',
);
foreach ($form_fields as $field) {
$field_id = $field[ 'id' ];
$field_key = $field[ 'key' ];
$field_value = $field[ 'value' ];
if ($field_id == 136) {
$post_fields['post_title'] = $field_value;
}
}
wp_insert_post($post_fields, true);
}

Wordpress custom post type permalink parent/child but different types

In wordpress I created 2 custom post types: company and offer
offers are linked to companies with a customfield called companyname.
So if an offer called 'My offer' is linked to Company 'ACME', both posttype contain a customfield called 'companyname' with value of ACME.So far no problem.
I registered both custom posttypes with a default slug 'company' and 'offer'
The shop permalink url is now: www.mywebsite.nl/company/acme/
The offer permalink url is now: www.mywebsite.nl/offer/my-offer/
What I want is the offer permalink is changed to www.mywebsite.nl/company/acme/my-offer/
I searched a lot through filters/action/hooks (and stackoverflow), but I cannot find a solution. I even tried to make the post-types hierarchical, but in an 'offer' posttype I cannot make a 'company' posttype the parent.
How can get the url structure I need?
There's two things you need to solve.
First, you need the url for the offers to be changed to your new form. This is done by specifying a slug that you can modify for each of the offers.
When registering the post type, add a variable to the slug:
// add modified
register_post_type('company_offer_type',array(
...
'rewrite' => array(
'slug' => 'company/%company%'
),
));
// filter to rewrite slug
add_filter( 'post_type_link', 'postTypeLink', 10, 2);
function postTypeLink($link, $post)
{
if (get_post_type($post) === 'company_offer_type') {
$company = // get company from $post
$link = str_replace('%company%', $company, $link);
}
return $link;
}
Second, you want wordpress to redirect incoming url requests to this post. Do this by adding a rewrite rule on the init action:
// add rewrite rule for custom type
add_rewrite_rule(
'company/(.*)/(.*)',
'index.php?post_type=company_offer_type&name=$matches[2]',
'top'
);
Here are two articles with more info:
http://code.tutsplus.com/articles/the-rewrite-api-the-basics--wp-25474
http://code.tutsplus.com/articles/the-rewrite-api-post-types-taxonomies--wp-25488

Using Dates in Custom post_type Permalinks in Wordpress 3.0

I'm adding a custom post_type to Wordpress, and would like the permalink structure to look like this:
/%post_type%/%year%/%monthnum%/%postname%/
I can't figure out how to add the date tags. Using this code, gives me /my_type/example-post-slug/:
register_post_type( 'customtype', array(
...other options...
'rewrite' => array('slug' => 'my_type'),
));
How do I include the dates?
You can achieve this with the plugin Custom Post Type Permalinks. Just install the plugin and change the permalink format in the settings.
You need to add WordPress structure tags to your rewrite attribute like so:
register_post_type('customtype',array(
....
'rewrite' => array('slug' => 'customtype/%year%/%monthnum%','with_front' => false)
));
Then add a post_type_link filter to rewrite the structure tags in your URLs for the custom post, so that the tags work:
function custompost_post_type_link($url, $post) {
if ( 'customtype' == get_post_type($post) ) {
$url = str_replace( "%year%", get_the_date('Y'), $url );
$url = str_replace( "%monthnum%", get_the_date('m'), $url );
}
return $url;
}
add_filter('post_type_link', 'custompost_post_type_link', 10, 2);
You can refer to this article for copypasta code (encapsulated in a class though) on creating custom posts like this. The article has a few extra bits of explanation and a few pieces of additional functionality too: https://blog.terresquall.com/2021/03/making-date-based-permalinks-for-custom-posts-in-wordpress/
EDIT: By the way, you'll also need to flush your permalinks after you're done for this to work.
I have found a partial solution which allows for the permalink to be recognized and preserved upon loading the page in the address bar, but not updated in the edit screen or other links to the post on the site.
Add the following to functions.php or a site specific plugin, replacing example-post-type with the identifier of your post type.
function example_rewrite() {
add_rewrite_rule('^example-post-type/([0-9]{4})/([0-9]{1,2})/([^/]*)/?','index.php?post_type=example-post-type&year=$matches[1]&monthnum=$matches[2]&name=$matches[3]','top');
}
add_action('init', 'example_rewrite');
This uses the Rewrite API documented here
To find more tips on understanding the process see here.
One thing to bear in mind is no matter how you do this, it is impossible for two posts to have the same slug, even if they have different dates. This is because if the permalink scheme is ever changed, they could clash and cause errors.
Use this it's working 100% :
'rewrite' => array('slug'=>date('Y').'/'.date('m').'/custom_post_type_slug','with_front'=>true)

Resources