Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple Fields examples: - how to hide post connector drop down on edit post screen using PHP - how to select post connector for a post using PHP
<?php
/**
* Set post connector for a post using PHP.
*
* This way it's possible to select a post connector for posts based on their ID, categories, tags, post type, and so on
*
* This function is using the filter simple_fields_get_selected_connector_for_post that is called when
* simple fields determines what connector it should use for each post.
* It passes the connector slug and the post object and you should return the slug of the conector that you want to use.
*
*/
function example_simple_fields_modify_connector_for_post($connector, $post) {
// Some examples to set the post connector comes here
// Plesae note that your're not limited to these,
// you can basically check everything for a post, like terms, categories, featured image, title...
if ( "attachments" === $post->post_type ) {
// If post type is "attachments" then use the attachments-post connector
$connector = "post_connector_attachments";
} else if ( 72 === $post->post_parent ) {
// if post has a parent post with id 72 then use another connector
$connector = "my_other_post_connector";
} else if ( "about" === $post->post_name ) {
// if post has a the slug "about"
$connector = "about_page_post_connector";
}
return $connector;
}
add_filter("simple_fields_get_selected_connector_for_post", "example_simple_fields_modify_connector_for_post", 10, 2);
/**
* By default Simple Fields shows a meta box on the edit post screen with a dropdown with
* all the post connectors that are possible to set for that post.
*
* By using the filter "simple_fields_add_post_edit_side_field_settings" we can hide that box
* and making the GUI a bit cleaner.
*
* Works best if you use the simple_fields_get_selected_connector_for_post-filter above to set
* the post connector using PHP, since then the user should not be able to try to change the
* connector to something else.
*
*/
function example_simple_fields_modify_post_edit_side_field_settings($bool_add, $post) {
// Don't show the dropdown if the current post has id 123
if ( 123 === $post->ID ) {
$bool_add = false;
}
return $bool_add;
}
add_filter("simple_fields_add_post_edit_side_field_settings", "example_simple_fields_modify_post_edit_side_field_settings", 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment