Use Carbon Fields in an included plugin file - wordpress

I am using the Carbon Fields plugin (not composer). I am building a large site and will have many functions to add fields to lots of different pages. I don't like to have extremely long files that make it harder to find the code for each page. If everything is in the main plugin file, it works fine. But if I try to require_once() or include() another file that contains Carbon Fields classes, I get the error: "class Container not found".
Is there a way to get the classes to be available in included files instead of everything having to be in the main file? I have searched the documentation and can find nothing on this.

Finally figured this out for anyone else running into an issue. Each included file that requires Carbon Fields classes must have the following at the beginning of the file:
require_once(WP_PLUGIN_DIR . '/carbon-fields/carbon-fields-plugin.php');
use Carbon_Fields\Container;
use Carbon_Fields\Field;
After this, you can start your functions and such. I am adding some additional coding on the extension plugin file to ensure the Carbon Fields plugin is activated to keep things from breaking.

You can include your file directly in functions.php like this (after installing the plugin):
require_once get_template_directory() . '/assets/inc/your-file.php';
Just replace /assets/inc/ with your folder structure.
I'm guessing your real issue is not invoking a Container::make before calling Field::make and the reason you received the class error.
In other words, something like this would go in your included file:
use Carbon_Fields\Container;
use Carbon_Fields\Field;
Container::make( 'post_meta', __( 'Page Options', 'crb' ) )
->where( 'post_type', '=', 'page' ) // only show our new fields on pages
->add_fields( array(
Field::make( 'complex', 'slider', 'Slides' )
->set_layout( 'tabbed-horizontal' )
->add_fields( array(
Field::make( 'text', 'slider_title', __( 'Slide Title' ) ),
Field::make( 'image', 'slider_media', __( 'Slide Media' ) )
->set_type( array( 'image', 'video' ) )
) ),
) );
The Container class must be called before the Field class or you will get the error you mentioned.

Related

Defining page layout for defualt post formats (aside)

single-aside.php defined but still using single.php for my post that having aside format.
Hi i used post-formats(aside) for some of my post and using single.php for styling them in the past despite defining a new post-type , the problem is now i want to use another page layout diffrent than my other posts and pages just for this aside type , but when i create single-aside.php wordpress doesnt use this page , and i flushed away my permalinks thats not the problem. do you think single page is defineable for defualt post formats like aside, video,quote and... ? any idea?
As per documentation you can use has_post_format() see more here https://codex.wordpress.org/Post_Formats under Using Formats.
Since the post formats aren't custom post types, you would need to add it to single.php:
if ( has_post_format( 'aside' )) {
//Your custom code for aside goes here
}
Alternatively, if you would like to use a different php file, you can create your custom post type and give support for Post formats.
Register with support:
add_action( 'init', 'aside_post_type' );
function aside_post_type() {
register_post_type( 'aside',
array(
'labels' => array( 'name' => __( 'Aside' ) ),
'public' => true,
'supports' => array('title', 'editor', 'post-formats') //this will give support for post formats
)
);
}
Then add single-aside.php to your theme and style this custom post type as needed.

WordPress is_search() function always false

I want to add some custom conditions and do some other things on a search WordPress results page. So, I’m checking the value of is_search() to make sure I’m only applying the condition at the right time. So in my child theme's functions.php I put:
if (is_search()) {
...
}
But this always returns false, even with a url like http://mysite/?s=something and my theme's search.php template is being used! Is this not valid to call from functions.php, or am I misunderstanding this function's purpose?
For that matter, looking at a template hierarchy like presented at https://wphierarchy.com, how does WP even know it’s a search results page? How does it know to proceed down the "search results" path? I’ve spent some time perusing the source code but haven’t been able to find the right spot yet.
I tried calling is_search() from search.php, and it works! From some more source code perusal, I discovered that you can't call is_search() from functions.php because functions.php is called too early in the process. Apparently functions.php is considered part of the "WordPress library" and is loaded in wp-blog-header.php line 13, while the template is called in line 19.
In between the two is the wp() function, which sets up the query. That query is actually set up in user.php line lines 1198-1212:
$args = array(
'post_type' => $this->post_type,
'post_name__in' => array( $this->request_type ),
'posts_per_page' => $posts_per_page,
'offset' => isset( $_REQUEST['paged'] ) ? max( 0, absint( $_REQUEST['paged'] ) - 1 ) * $posts_per_page : 0,
'post_status' => 'any',
's' => isset( $_REQUEST['s'] ) ? sanitize_text_field( $_REQUEST['s'] ) : '',
);
...
$requests_query = new WP_Query( $args );
To answer my second question (how does WordPress know it's a search), it’s as simple as having the "s=" query string parameter. Not sure what to do if you want a search result that does not have a search string (say a custom search based solely on pulldowns or something). If you need that, I would recommend looking thru the source file WP-query.php and seeing exactly what you need to pass. Don’t be scared of looking at the source code; It’s very educational!

TinyMCE Visual Editor Won't Save, PlainText Does?

I have TinyMCE in my Wordpress plugin, and have been having a super irritating problem with TinyMCE. It will save edits made in plaintext mode but won't save edit made in Visual mode (and throws an error).
This is the code I use to create the TinyMCE textarea. I name the field correctly to save and load from options.php (which works when it's just a plain textarea).
$x_options = get_option('editbox');
/*HTML form fields*/
$rc_initial_data = ( isset( $x_options['fluid_body'] ) ) ?$x_options['fluid_body']:'';
$settings = array( 'wpautop' => true, 'textarea_name' => 'editbox[fluid_body]', 'quicktags' => true, 'tinymce' => true, 'quicktags' => array('buttons' => 'em,strong,link,block,ul,ol,li,p,close,spell,img') );
wp_editor( $rc_initial_data, 'fluid_body', $settings );
The error I get is this:
Can't create duplicate variable: 'typeMap'
The error is in wp-tinymce.php (which loads a .js file). I use multiple TinyMCE-enabled fields, but they are all named uniquely so the "duplicate" thing shouldn't be a problem (and isn't, if it's plaintext).
Any ideas why the Visual editor isn't working? Everything is the current version, Wordpress, jQuery, etc.

Wordpress: functions.php Breaking Standard WP Functions

This issue is driving me crazy.
This morning, the Wordpress theme I'm creating included a very simple functions.php file, and my site worked properly. Today, I wrote a new function to iterate through categories and list certain posts, and that's the only change I made to functions.php.
After uploading the revised functions file to the server, I get a 500 error every time I access my site. I though, "OK, my new function broke something. I'll just remove it from functions.php and start over." However, after reverting to the original version of the file (the version that worked this morning), I'm still getting the 500 error.
If I remove functions.php from the server, my pages load (without their sidebars obviously, but they load). As soon as I upload the file again (same version that worked this morning), I get the 500 error.
I checked the error log, and I've found that, when I have functions.php on the server, Wordpress (or php more likely) can no longer find the get_template_directory_uri() function. I've done nothing to alter any of the standard Wordpress functions, and this only happens when I've got functions.php uploaded.
What might be causing functions.php to break the standard functions?
Here's my functions.php file:
<?php
/* *********************************************
* Create left-hand sidebar for single.php
*
* TODO: Finalize $sricks_single_left_sidebar.
* ********************************************/
$sricks_single_left_sidebar = array(
'name' => 'Left Sidebar',
'id' => 'lt-single',
'description' => 'This is the sidebar on the left side of the single.php template page.',
'before_widget' => '<!-- Left-Single Sidebar --><div class="lt-single">',
'after_widget' => '</div> <!-- End Left-Single Sidebar -->',
'before_title' => '<h1>',
'after_title' => '</h1>',
);
register_sidebar( $sricks_single_left_sidebar );
/* *********************************************
* Create header_search sidebar to be used on
* all pages
* ********************************************/
$sricks_header_search_sidebar = array(
'name' => 'Header Search',
'id' => 'header-search',
'description' => 'This is the search box in the page header.',
'before_widget' => '<!-- Header-Search Sidebar --><div class="header-search">',
'after_widget' => '</div> <!-- End Header-Search Sidebar -->',
'before_title' => '<h1>',
'after_title' => '</h1>',
);
register_sidebar( $sricks_header_search_sidebar );
?>
To find the cause of issues in WordPress when you get 500 level errors, a blank white screen, or any other strange or unexpected behavior, define the WP_DEBUG constant in wp-config.php.
Like this:
define( 'WP_DEBUG', true );
If you're developing or making changes to a site, it's usually a good idea to enable this feature until you're done editing. It can make it very easy to spot and correct mistakes that could otherwise vex for hours.
Thanks everyone, especially Spencer Cameron. It turns out I forgot the open and close parentheses in my new function's declaration. Wordpress reads functions.php before any of the built-in functions, so it just got stuck there.
When I submitted my code, I left out my new function because I had deleted everything from the body of the function; what could go wrong? Lesson learned...

Wordpress comment_form( ) won't accept my arguments

I'm trying to diligently follow what the codex says about modifying the appearance of the comments form (located here) but I can't get it to work. The default comments form appears despite my array of arguments that should change it.
In the template I have: comments_template('mycomments.php');
Then...
<?php
//inside my mycomments.php file
$comment_args = array (
'fields' => array('author', 'email'),
'must_log_in' => true,
'title_reply' => 'Review this product',
'label_submit' => 'Submit',
);
comment_form($comment_args, $post->ID); ?>
Am I doing something wrong?
I figured it out. There was nothing wrong with my arguments. Instead... I needed a / infront of the relative link to my customized comments template:
comments_template('/mycomments.php');
Moral of the story: If you're having trouble getting the template to work (at all), make sure you're actually loading the right template file!

Resources