Adding graphql support for custom posts - wp-graphql

I have installed WP GraphQL plugin and it works fine for pages and posts. However its not working for custom posts.
for example,
query getPages{
pages(first: 1000) {
edges {
node {
uri
}
}
}
}
works.
query getPastEvents{
past-events(first: 1000) {
edges {
node {
uri
}
}
}
}
not works.
I have read this doc https://www.wpgraphql.com/docs/custom-post-types/ but still not understood exact process.
update: i added the below code in wp-includes/plugin.php and its now showing careCenter, careCenterPages in WP GraphQL IDE but its returning no data
// WP GRAPH QL
add_action( 'init', function() {
register_post_type( 'care_center', [
'show_ui' => true,
'labels' => [
//#see https://developer.wordpress.org/themes/functionality/internationalization/
'menu_name' => __( 'CareCenter', 'care_center' ),
],
'show_in_graphql' => true,
'hierarchical' => true,
'graphql_single_name' => 'careCenter',
'graphql_plural_name' => 'careCenterPages',
] );
} );
add_filter( 'register_post_type_args', function( $args, $post_type ) {
// Change this to the post type you are adding support for
if ( 'care_center' === $post_type ) {
$args['show_in_graphql'] = true;
$args['graphql_single_name'] = 'careCenter';
$args['graphql_plural_name'] = 'careCenterPages';
}
return $args;
}, 10, 2 );
am attaching few screenshots below for more understanding...
I want graphql support for custom posts just like pages & posts.

Related

Unable to hide/disable custom metabox in attachment

I have a stack with https://lumberjack.rareloop.com/ and use it to create a custom taxonomy for WP attachement. I use it to filter my media in admin panel and hide them to non-authorize account. I would like to hide meta box for the same account but can't find a way to make it work.
My custom taxonomy:
namespace App\Taxonomy;
use Rareloop\Lumberjack\Models\AbstractTerm;
class AttachmentType extends AbstractTerm
{
public static function getTaxonomyObjectTypes(): array
{
return [
'attachment',
];
}
public static function getTaxonomy(): string
{
return 'attachment_type';
}
protected static function getTaxonomyConfig(): array
{
return [
'hierarchical' => false,
'public' => false,
'publicly_queryable' => false,
'show_ui' => true,
'query_var' => 'media',
'meta_box' => 'radio',
'meta_box_cb' => false, // not working
];
}
Also try to use admin_menu hook in functions.php with no success :
add_action('admin_menu','remove_post_meta_boxes');
function remove_post_meta_boxes() {
remove_meta_box( 'submitdiv','attachment','normal' ); // working
remove_meta_box( 'attachment_typediv','attachment','normal' ); // not working
}
Metabox still visible with id attachment_typediv in html. Search for hours and tried many things but couldn't find a solution.
Any help is greatly appreciated!

Wordpress stripping attributes even when explicitly allowed

I've looked through every guide on allowing HTML tags and attributes to Wordpress posts, but it's still stripping the allow attribute from iframes. When echoing the allowedposttags global, it looks like iframe[allow] is set to true. TinyMCE's settings show that extended_valid_elements is set to allow all *[*]. The POST data to the Wordpress update endpoint is not stripped. I'd really appreciate any help in identifying the problem. Here's the plugin I put together based on a few guides:
<?php
/**
* Plugin Name:Allow Tags
* Description: Allows one to add "invalid" and custom HTML elements to the Wordpress editor.
* Version: 0.1
* Author: Sam Hill
* with help from http://www.engfers.com/2008/10/16/how-to-allow-stripped-element-attributes-in-wordpress-tinymce-editor/ and many other sources
*/
class AllowTags {
public static $instance = null;
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
private function __construct() {
add_filter('wp_kses_allowed_html', 'custom_wpkses_post_tags', 10, 2 );
add_filter('tiny_mce_before_init', 'tinymce_allow_iframe');
add_action('init', 'allow_iframe_attributes', 10, 2);
}
}
AllowTags::get_instance();
function tinymce_allow_iframe($init) {
$options = '*[*]';
$init['valid_elements'] = $options;
$init['extended_valid_elements'] = $options;
$init['verify_html'] = false;
return $init;
}
function custom_wpkses_post_tags($allowed, $context){
if (is_array($context)) {
return $allowed;
}
if ($context === 'post') {
$allowed['iframe'] = array(
'src' => true,
'height' => true,
'width' => true,
'frameborder' => true,
'allowfullscreen' => true,
'allow' => true
);
}
return $allowed;
}
function allow_iframe_attributes( $string ) {
global $allowedposttags;
$allowedposttags['iframe'] = array(
'allow' => true,
'align' => true,
'frameborder' => true,
'height' => true,
'width' => true,
'sandbox' => true,
'seamless' => true,
'scrolling' => true,
'srcdoc' => true,
'src' => true,
'class' => true,
'id' => true,
'style' => true,
'border' => true,
);
return $string;
}
}

Wordpress Buddypress always show members list of group

I'm working on a Wordpress/Buddypress project and I would like to make the members list of group always visible whether or not the group is private and the logged member belong to the group.
I was thinking I had to change access and visibility of the nav items so I did that:
function change_access_group_nav_tabs() {
if(bp_is_group()) {
buddypress()->groups->nav->edit_nav( array('visibility' => 'public'), 'members', bp_current_item() );
buddypress()->groups->nav->edit_nav( array('access' => 'anyone'), 'members', bp_current_item() );
}
}
add_action( 'bp_actions', 'change_access_group_nav_tabs' );
But it didn't work…
Any suggestion how to proceed?
I found a workaround, I'am quite not completely satisfied but...
First of all, disable members list on group:
function change_access_group_nav_tabs() {
if(bp_is_group()) {
buddypress()->groups->nav->edit_nav( array( 'user_has_access' => false ), 'members', bp_current_item() );
}
}
add_action( 'bp_actions', 'change_access_group_nav_tabs' );`
(btw, setting the value to true actually make the the nav items always here, but we still can't access the group list on click)
And then I simply add a custom BP Group Extension to make my own members list:
class Group_Extension_List_Members extends BP_Group_Extension {
function __construct() {
$args = array(
'slug' => 'members-list',
'name' => 'Membres',
'access' => array( 'anyone'),
'show_tab' => array( 'anyone'),
'nav_item_position' => 12,
'screens' => array(
'create' => array(
'enabled' => false
),
'edit' => array(
'enabled' => false
),
)
);
parent::init( $args );
}
function display( $group_id = NULL ) {
//Remove user who do not belong to the group on members loop
function filter_for_groups( $members_template_has_members, $members_template, $r ) {
for ($i=sizeof($members_template->members)-1; $i >= 0 ; $i--) {
$user_id = $members_template->members[$i]->id;
if(!groups_is_user_member( $user_id, bp_get_group_id() )){
$members_template->member_count = $members_template->member_count-1;
array_splice($members_template->members, $i, 1);
}
}
if ($members_template->member_count <= 0) {
return '';
} else {
return $members_template_has_members;
}
};
add_filter( 'bp_has_members', 'filter_for_groups', 10, 3 );
require('/Your/theme/custom/members/loop/members-loop.php');
}
}
bp_register_group_extension( 'Group_Extension_List_Members' );
Hope it will help other in the future, And I'm still open to know the good way to proceed.

Woocommerce 3.1 product variation meta data

I have currently added metadata to a product variation woocommerce_product_after_variable_attributes and woocommerce_save_product_variation Guide here.
function custom_woocommerce_product_after_variable_attributes($loop, $variation_data, $variation)
{
woocommerce_wp_select([
'id' => 'field_1['.absint($variation->ID).']',
'label' => 'Field 1 ',
'value' => get_post_meta(absint($variation->ID), 'field_1', true),
'options' => [
'1' => '1',
'2' => '2',
'3' => '3',
],
]);
}
add_action('woocommerce_product_after_variable_attributes', 'custom_woocommerce_product_after_variable_attributes', 10, 3);
function custom_woocommerce_save_product_variation($post_id)
{
$field1 = $_POST['field_1'][$post_id];
if (! empty($field1)) {
update_post_meta(absint($post_id), 'field_1', esc_html($field1));
}
}
add_action('woocommerce_save_product_variation', 'custom_woocommerce_save_product_variation', 10, 2);
Then in the js hooked into single_variation_wrap when the variation was changed. This was working fine in 3.0.5 but since updating to 3.1.1 in the js i am no longer getting the custom meta_data for variations.
$('.single_variation_wrap').on('show_variation', function(event, variation) {
console.log(variation.meta_data);
});
The meta_data information no longer exists.
How can this be fixed?
I was able to fix this by adding a filter.
function custom_woocommerce_available_variation($variations, $product, $variation)
{
$metadata = $variation->get_meta_data();
if (!empty($metadata)) {
$variations = array_merge($variations, [
'meta_data' => $metadata,
]);
}
return $variations;
}
add_filter('woocommerce_available_variation', 'custom_woocommerce_available_variation', 10, 3);

Wordpress custom posts : restrict "meta_key" to "post_type"

I've built a plugin that has a decent amount of users. I've built it based on number of examples here and there + the mighty codex. I'm using a custom post type to store values and reuse them in the front end to display stuff.
Some time ago, I noticed a terrible behavior. EVERY TIME a (regular or custom) post is created, it generates additional meta_keys in WP_postmeta. If I deactivate the plugin, that pattern stops. I'm basically filling my users databases with empty values as shown here which crancks up the number of meta_id too. Not good.
I've initially thought of not storing empty fields, but that's not the "right" way of doing. How do I limit the submission of these metakeys to my custom post type only ?
Anything fishy with my post-type declaration ?
function booking_pluginbox_init() {
$labels = array(
'name' => __('Booking.com Affiliate Plugin', 'post type general name'),
'singular_name' => __('Search Box', 'post type singular name'),
'add_new' => __('Add new', 'member'),
'add_new_item' => __('Create a new Search box'),
'edit_item' => __('Edit a Search box'),
'new_item' => __('Create a new Search box'),
'view_item' => null,
'search_items' => __('Search'),
'not_found' => __('No result found!'),
'not_found_in_trash' => __('No Search Boxes in the trash!'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
// 'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array('slug' => 'booking-pluginbox'),
'capability_type' => 'post',
'hierarchical' => false,
'has_archive' => true,
'menu_position' => 100,
'menu_icon' => plugins_url('/includes/images/booking_plugin-icon.png', dirname(__FILE__)),
'supports' => array('title'),
'register_meta_box_cb' => 'booking_pluginbox_meta_boxes'
);
register_post_type('booking-pluginbox',$args);
add_action( 'save_post', 'booking_pluginbox_save_postdata' );
}
add_action('init', 'booking_pluginbox_init');
Here's the link to the SVN trunk : http://plugins.svn.wordpress.org/bookingcom-affiliate-plugin/trunk/
UPDATE1
Sure, here's the post_save function
function booking_pluginbox_save_postdata($post_id) {
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// Check user permissions
if ($_POST['post_type'] == 'page') {
if (!current_user_can('edit_page', $post_id)) return $post_id;
} else {
if (!current_user_can('edit_post', $post_id)) return $post_id;
}
// OK, we're authenticated: we need to find and save the data
$current_bp_AID = get_post_meta($post_id, 'booking_plugin_AID', false);
$new_bp_AID = (isset($_POST['booking_plugin_AID'])) ? $_POST['booking_plugin_AID'] : '';
$current_bp_TRACKING = get_post_meta($post_id, 'booking_plugin_TRACKING', false);
$new_bp_TRACKING = (isset($_POST['booking_plugin_TRACKING'])) ? $_POST['booking_plugin_TRACKING'] : '';
$current_bp_DESTINATION = get_post_meta($post_id, 'booking_plugin_DESTINATION', false);
$new_bp_DESTINATION = (isset($_POST['booking_plugin_DESTINATION'])) ? $_POST['booking_plugin_DESTINATION'] : '';
...
$current_bp_CSS_override = get_post_meta($post_id, 'booking_plugin_CSS_override', false);
$new_bp_CSS_override = (isset($_POST['booking_plugin_CSS_override'])) ? $_POST['booking_plugin_CSS_override'] : '';
$current_bp_jqtheme = get_post_meta($post_id, 'booking_plugin_jqtheme', false);
$new_bp_jqtheme = (isset($_POST['booking_plugin_jqtheme'])) ? $_POST['booking_plugin_jqtheme'] : '';
booking_pluginbox_clean($new_bp_AID);
booking_pluginbox_clean($new_bp_TRACKING);
booking_pluginbox_clean($new_bp_DESTINATION);
...
booking_pluginbox_clean($new_bp_wpx);
booking_pluginbox_clean($new_bp_CSS_override);
booking_pluginbox_clean($new_bp_jqtheme);
if (!empty($current_bp_AID)) {
if (is_null($new_bp_AID)) {
delete_post_meta($post_id,'booking_plugin_AID');
} else {
update_post_meta($post_id,'booking_plugin_AID',$new_bp_AID);
}
} elseif (!is_null($new_bp_AID)) {
add_post_meta($post_id,'booking_plugin_AID',$new_bp_AID,true);
}
if (!empty($current_bp_TRACKING)) {
if (is_null($new_bp_TRACKING)) {
delete_post_meta($post_id,'booking_plugin_TRACKING');
} else {
update_post_meta($post_id,'booking_plugin_TRACKING',$new_bp_TRACKING);
}
} elseif (!is_null($new_bp_TRACKING)) {
add_post_meta($post_id,'booking_plugin_TRACKING',$new_bp_TRACKING,true);
}
if (!empty($current_bp_DESTINATION)) {
if (is_null($new_bp_DESTINATION)) {
delete_post_meta($post_id,'booking_plugin_DESTINATION');
} else {
update_post_meta($post_id,'booking_plugin_DESTINATION',$new_bp_DESTINATION);
}
} elseif (!is_null($new_bp_DESTINATION)) {
add_post_meta($post_id,'booking_plugin_DESTINATION',$new_bp_DESTINATION,true);
}
...
if (!empty($current_bp_CSS_override)) {
if (is_null($new_bp_CSS_override)) {
delete_post_meta($post_id,'booking_plugin_CSS_override');
} else {
update_post_meta($post_id,'booking_plugin_CSS_override',$new_bp_CSS_override);
}
} elseif (!is_null($new_bp_CSS_override)) {
add_post_meta($post_id,'booking_plugin_CSS_override',$new_bp_CSS_override,true);
}
if (!empty($current_bp_jqtheme)) {
if (is_null($new_bp_jqtheme)) {
delete_post_meta($post_id,'booking_plugin_jqtheme');
} else {
update_post_meta($post_id,'booking_plugin_jqtheme',$new_bp_jqtheme);
}
} elseif (!is_null($new_bp_jqtheme)) {
add_post_meta($post_id,'booking_plugin_jqtheme',$new_bp_jqtheme,true);
}
return $post_id;
}
Thanks for your directions !!
Greg
At the moment your function will run for every save post action. You should query if it is the custom post type before continuing on. Another way and also a good idea is to add a nounce.
Also you dont need add_post_meta instead use update_post_meta which will create a new meta key if it dosent exist.
function booking_pluginbox_save_postdata($post_id) {
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
// check post type
if ($_POST['post_type'] != 'booking-pluginbox') {
return $post_id;
}
// Check user permissions -- it was just checking if the post type was a page.
if ($_POST['post_type'] == 'booking-pluginbox') {
if (!current_user_can('edit_page', $post_id)) return $post_id;
} else {
if (!current_user_can('edit_post', $post_id)) return $post_id;
}
// no need to pull the old values unless you are working with them.
if ($new_bp_AID) { // update post meta will create a new meta if non-existant.
update_post_meta($post_id,'_booking_plugin_AID',$new_bp_AID); // add "_" if there should only be 1 key
} else {
delete_post_meta($post_id,'_booking_plugin_AID');
}
return $post_id;
}

Resources