Atanas Angelov
Forum Replies Created
-
Forum: Plugins
In reply to: [Carbon Fields] Error When Including Carbon Fields in PluginUpdating this ticket since we just released 2.0.2 which resolves the issue with the headers – https://github.com/htmlburger/carbon-fields/releases/tag/2.0.2
The plugin loader file is now it’s own separate package so requiring htmlburger/carbon-fields won’t cause issues like this anymore.
This also means you do not need thescripts
andextra
declarations when including Carbon Fields in your plugin or theme.- This reply was modified 7 years, 8 months ago by Atanas Angelov.
Forum: Plugins
In reply to: [Carbon Fields] Error When Including Carbon Fields in PluginHi @uamv,
For the time being, manually removing the headers is the best solution.
We are currently looking at different solutions to find a better one and hopefully we will have it ready for the next minor release.
FYI, you can add this to your composer.json so no duplicate
wp-content/plugins
directories are created inside your plugin directory:"extra": { "installer-paths": { "vendor/{$vendor}/{$name}/": ["type:wordpress-plugin"] } }
Forum: Plugins
In reply to: [Carbon Fields] CF1.6 and l10nHi @hermit931,
Since Theme Option containers resolve their internal slug/filename from their title, translating the title will cause this issue.
The solution is to use theset_page_file()
method to define your own slug/filename so the container does not rely on the title e.g.:Container::make( 'theme_options', __( 'My Translated Container Title', 'crb' ) ) ->set_page_file( 'my-container' ) ->add_fields( array( ... ) );
Forum: Plugins
In reply to: [Carbon Fields] Fatal Error When Using Carbon Fields 2.0.1Hi @uamv,
You should not be including
carbon-fields-plugin.php
– make sure you’ve required Carbon Fields using composer and that you’ve loaded composer’s autoloader instead.The Quickstart page we have can be handy – it guides you through the setup inside a theme but the same pretty much applies to plugins:
https://carbonfields.net/docs/carbon-fields-quickstart/?crb_version=2-0-0Also note that 2.0 brings in a number of breaking changes that you should be aware of – it’s best if you review the upgrade guide:
https://carbonfields.net/docs/carbon-fields-upgrade-guide/?crb_version=2-0-0Forum: Plugins
In reply to: [Carbon Fields] New to carbon fields -need to add image alt tagCarbon Fields provide you with an attachment id – this is sufficient to pull any and all attachment data through various WordPress functions (as discussed in the thread I linked in my previous message).
There is no need of an additional alt field since WordPress already handles titles, alts and other data through their attachments:
If you fill in that field, the following code is sufficient to pull the image with alt:
echo wp_get_attachment_image( carbon_get_post_meta( 'your_field_name_here' ) );
Forum: Plugins
In reply to: [Carbon Fields] New to carbon fields -need to add image alt tagHi @phillystyle123,
This is not really in the scope of Carbon Fields as your issue is with WordPress’ wp_get_attachment_image() not always adding an “alt” attribute.
You can refer to https://wordpress.stackexchange.com/questions/1051/how-to-retrieve-an-image-attachments-alt-text where they discuss this and add alternative solutions.
Forum: Plugins
In reply to: [Carbon Fields] Carbon Fields and SearchThe gist you shared only contains a single reference to
carbon_get_*
and from what I can see it is used correctly and should return an array of section entries as long as it is used in The Loop.As for your earlier snippet –
carbon_get_the_post_meta( 'content_section_column1' )
– you cannot access fields inside a complex directly. You must get the root complex withcarbon_get_*
and then access the property you are interested in using the returned array.Forum: Plugins
In reply to: [Carbon Fields] Carbon Fields and SearchCan you share your container and field definitions code as well?
Forum: Plugins
In reply to: [Carbon Fields] Carbon Fields and SearchThere isn’t an automatic way to query what fields belong to what post type. However, you can add your own logic in your search template loop based on
get_post_type()
.For example:
<?php $post_type = get_post_type(); if ( $post_type === 'post' ) { echo carbon_get_the_post_meta( 'crb_some_post_field' ); } else if ( $post_type === 'page' ) { echo carbon_get_the_post_meta( 'crb_some_page_field' ); echo carbon_get_the_post_meta( 'crb_some_page_field_2' ); } // etc ... ?>
Forum: Plugins
In reply to: [Carbon Fields] Carbon Fields and SearchHi @tinothepro ,
Carbon Fields do not automatically output anything to the front-end (header/footer scripts fields being the only exceptions) so you have to use the provided collection of getter methods (e.g.
carbon_get_post_meta
,carbon_get_theme_option
etc.) to output values in your theme.Forum: Plugins
In reply to: [Carbon Fields] Multistyle complex(?) fieldHi @giray,
Yes, this is something you can do with Carbon Fields.
If you have a specific list of fields that will never change you can use the following snippets:Add this to the top of your theme’s functions.php file:
<?php use Carbon_Fields\Field; use Carbon_Fields\Container; add_action( 'carbon_register_fields', 'crb_attach_post_meta' ); function crb_attach_post_meta() { Container::make( 'post_meta', __( 'Post Options', 'crb' ) ) ->show_on_post_type( 'post' ) ->add_fields( array( Field::make( 'separator', 'crb_reference_separator', 'Reference' ), Field::make( 'text', 'crb_reference_title', 'Title' ) ->set_width( 25 ), Field::make( 'text', 'crb_reference_year', 'Year' ) ->set_width( 25 ), Field::make( 'text', 'crb_reference_name', 'Name' ) ->set_width( 25 ), Field::make( 'text', 'crb_reference_country', 'Country' ) ->set_width( 25 ), ) ); }
Add this to your post loop where you wish to output the combined reference:
<?php $reference = array( carbon_get_the_post_meta( 'crb_reference_title' ), '<strong>' . carbon_get_the_post_meta( 'crb_reference_year' ) . '</strong>', carbon_get_the_post_meta( 'crb_reference_name' ), '<strong><em>' . carbon_get_the_post_meta( 'crb_reference_country' ) . '</em></strong>', ); echo '<p>' . implode( ', ', $reference ) . '.</p>'; ?>
Alternatively, if you wish to have multiple references per post you can wrap the 4 fields in a complex field. If this is the case use these snippets instead:
Add this to the top of your theme’s functions.php file:
<?php use Carbon_Fields\Field; use Carbon_Fields\Container; add_action( 'carbon_register_fields', 'crb_attach_post_meta' ); function crb_attach_post_meta() { Container::make( 'post_meta', __( 'Post Options', 'crb' ) ) ->show_on_post_type( 'post' ) ->add_fields( array( Field::make( 'complex', 'crb_references', 'References' ) ->add_fields( array( Field::make( 'text', 'title', 'Title' ) ->set_width( 25 ), Field::make( 'text', 'year', 'Year' ) ->set_width( 25 ), Field::make( 'text', 'name', 'Name' ) ->set_width( 25 ), Field::make( 'text', 'country', 'Country' ) ->set_width( 25 ), ) ) ) ); }
Add this to your post loop where you wish to output the combined reference:
<?php $references = carbon_get_the_post_meta( 'crb_references', 'complex' ); foreach ( $references as $reference_values ) { $reference = array( $reference_values['title'], '<strong>' . $reference_values['year'] . '</strong>', $reference_values['name'], '<strong><em>' . $reference_values['country'] . '</em></strong>', ); echo '<p>' . implode( ', ', $reference ) . '.</p>'; } ?>
- This reply was modified 7 years, 9 months ago by Atanas Angelov.
Forum: Plugins
In reply to: [Carbon Fields] questions for the security of carbon fieldsCarbon Fields use nonces for every request and add handlers only on WordPress hooks which means that WordPress security is not circumvented with any special request handling.
Note that Carbon Fields is only responsible for secure storage of the data users enter into fields – the security of how you output values from Carbon Fields is up to you (i.e. sanitize user input when outputting values in your theme so untrusted users do not get to inject iframes or script tags for example).
Forum: Plugins
In reply to: [Carbon Fields] how retrieve a thumb term meta for admin areaHi @stardeuche,
File/Image fields only return the selected attachment ID as their value so you have to use wp_get_attachment_image() to transform that into an
tag.
I believe this is what you need:
<?php function my_custom_taxonomy_columns_content($columns) { $columns['featured-img-dossier'] = __('Image Dossier'); return $columns; } add_filter('manage_edit-dossier_cat_columns', 'my_custom_taxonomy_columns_content'); function add_content_custom_taxo_column($content, $column_name, $term_id) { if ( 'featured-img-dossier' !== $column_name ) { return $content; } $image = carbon_get_term_meta( $term_id, 'your_image_field_name_here' ); if ( $image ) { echo wp_get_attachment_image( $image, 'category-thumb' ); } else { echo 'pas d\'image'; } } add_action('manage_dossier_cat_custom_column','add_content_custom_taxo_column', 10, 3);
Hi @peperene5150,
You are close to what you need but calling foreach inside an array declaration is not valid php syntax which is what causes the error.
I’ve updated your code here which should do the job:
function get_category_options() { $argsCat = array( 'orderby' => 'ID', 'order' => 'ASC' ); $categories = get_categories( $argsCat ); $options = array(); foreach ($categories as $term) { $options[ $term->term_id ] = $term->name; } return $options; } // My field Container::make('post_meta', __( 'My field' ) ) ->show_on_post_type('page') ->add_fields( array( // gallery Field::make( 'complex', 'h_hero_slider', __( 'Ajouter un slide' ) ) ->add_fields( '', [ Field::make( 'select', 'cat', 'Catégorie slider' ) ->set_width(50) ->add_options( 'get_category_options' ) ->set_required( true ), ]), ));
In this case we’re making use of the ability of
add_options
to accept a function as it’s argument so that it lazyloads the options instead of loading them on every pageload.- This reply was modified 7 years, 9 months ago by Atanas Angelov.
Forum: Plugins
In reply to: [Carbon Fields] Use get_posts and order by carbon fieldsHi @joelschmid,
Let me know if this is the correct situation you have:
1. You have thecrb_ws-id
field added to all posts
2. You wish to pull all posts and order them based on the value they have in theircrb_ws-id
fieldTo achieve the above use this:
$args = array( 'posts_per_page' => 5, 'offset' => 0, 'category' => $settings->tax_post_category, 'meta_key' => '_crb_ws-id', // NOTE: don't forget the leading underscore - meta keys are stored with an underscore prepended to the field name 'orderby' => 'meta_value_num', // NOTE: change to 'meta_value' if the values in crb_ws-id are not numeric 'order' => 'DESC', ); $myposts = get_posts( $args );