Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose in REST API #71

Open
jonferreira opened this issue Sep 14, 2018 · 1 comment
Open

Expose in REST API #71

jonferreira opened this issue Sep 14, 2018 · 1 comment

Comments

@jonferreira
Copy link

Hi,

I'm working on importing 100s of pages to wordpress and some have in-line CSS/JS which I'll need to import.

I'm using the WP REST API to do this and I'd need to know how / if it's possible to use the REST API to "paste" my CSS/JS code snippets per post.
Thanks

@jonferreira
Copy link
Author

jonferreira commented Sep 14, 2018

OK clearly it isn't so I've registered _SnS to be exposed in REST but still struggling unfortunatelly

static function init() {
		if ( is_admin() && ! ( defined('DISALLOW_UNFILTERED_HTML') && DISALLOW_UNFILTERED_HTML ) ) {
			/*	NOTE: Setting the DISALLOW_UNFILTERED_HTML constant to
				true in the wp-config.php would effectively disable this
				plugin's admin because no user would have the capability.
			*/
			include_once( 'includes/class-sns-admin.php' );
			SnS_Admin::init();
		}
		//register_theme_directory( WP_PLUGIN_DIR . "/" . basename( dirname( __FILE__ ) ) . '/theme/' );
		add_action( 'plugins_loaded', array( __CLASS__, 'upgrade_check' ) );

		add_filter( 'body_class', array( __CLASS__, 'body_classes' ) );
		add_filter( 'post_class', array( __CLASS__, 'post_classes' ) );

		add_action( 'wp_head', array( __CLASS__, 'styles' ), 11 );
		add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ), 11 );
		add_action( 'wp_head', array( __CLASS__, 'scripts_in_head' ), 11 );
		add_action( 'wp_footer', array( __CLASS__, 'scripts' ), 11 );

		add_action( 'plugins_loaded', array( __CLASS__, 'add_shortcodes' ) );
		add_action( 'widgets_init', array( __CLASS__, 'add_widget' ) );

		add_action( 'wp_enqueue_scripts', array( __CLASS__, 'register' ) );
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'register' ) );

		add_action( 'wp_print_styles', array( __CLASS__, 'theme_style' ) );
		add_action( 'wp_ajax_sns_theme_css', array( __CLASS__, 'theme_css' ) );
		add_action( 'wp_ajax_nopriv_sns_theme_css', array( __CLASS__, 'theme_css' ) );
		
		register_meta('post', '_SnS',
			[
				'sanitize_callback' => 'sanitize_array_field',
				'show_in_rest' => true,
				'single' => false
			]
		);
	}
	
	function sanitize_array_field( $meta_value ) {

		foreach ( (array) $meta_value as $k => $v ) {
			if ( is_array( $v ) ) {
				$meta_value[$k] =  sanitize_array_field( $v );
			} else {
				$meta_value[$k] = sanitize_text_field( $v );
			}
		}

		return $meta_value;

	}

And I get "array" instead of an actual Array

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant