WPGraphQL and Elementor getting page or post css and scripts - wordpress

Elementor is generating page specific styling and scripts inside head section of the WordPress page. Is there a way to read them to GraphQL?

I did the same for WpBakery pagebuilder in 2 steps:
register new field in graphQL with my CSS from editor, something like:
add_action( 'graphql_register_types', function() {
register_graphql_field( 'Page', 'customCSS', [
'type' => 'String',
'description' => __( 'Custom CSS from Editor', 'wp-graphql' ),
'resolve' => function() {
// call your function which return your custom css
return $this->addPageCustomCssToGraphql();
}
] );
} );
request them in Gatsby page and using Helmet plugin show your CSS in HEAD of your page.

Related

Custom page created by a wordpress plugin

I am trying to figure out how to create a Wordpress plugin that adds a page to the Wordpress website on which it is installed.
I came up with the following which works and adds the page.
However, it's far from what I am trying to achieve:
How to change the entire page? Not just the content? Right now, it has all the header and footer and navbar.
How to have PHP code in the page, not just static content?
Is it possible to have everything under a url (https://some-url.com/my-plugin/) routed to this same page?
For example:
https://some-url.com/my-plugin/ -> run my page
https://some-url.com/my-plugin/foo/ -> run my page
https://some-url.com/my-plugin/foo2/abc/ -> run my page
etc.
<?php
/**
* Plugin Name: MyPlugin
* Plugin URI: myplugin.com
* Description: MyPlugin
* Version: 1.0
* Author: Mike
* Author URI: myplugin.com
*/
define( 'MYPLUGIN__PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
function create_page() {
$post_data = array(
'post_title' => 'Test of my plugin',
# How to change the entire page? Not just the content?
# Also, how to have PHP code in the page, not just static content?
'post_content' => 'Place all your body content for the post in this line.',
'post_status' => 'publish', // Automatically publish the post.
'post_type' => 'page', // defaults to "post".
'post_name' => 'my-plugin', // url slug (will 'slugify' post_title if empty) https://some-url.com/my-plugin-page?/
);
// Lets insert the page now.
wp_insert_post( $post_data );
}
function plugin_activation() {
create__page();
}
function plugin_deactivation() {
}
register_activation_hook( __FILE__, 'plugin_activation' );
register_deactivation_hook( __FILE__, 'plugin_deactivation' );
You want wp_insert_post()
Usage:
$postarr = [
'post_title' => 'My Page Title', // <title> tag
'post_content' => '<p>page content</p>', // html for page content
'post_type' => 'page', // blog post is default
'post_name' => 'my-plugion-page', // url slug (will 'slugify' post_title if empty) https://some-url.com/my-plugion-page?/
];
$post_id = wp_insert_post($postarr);
For example, in your $postarr if you want it to be a page (not a blog post), use 'post_type' => 'page'. For the page title 'post_title' => 'My New Page' and so on.
See: https://developer.wordpress.org/reference/functions/wp_insert_post/
Updated for additions to user question
If you don't want it to follow the theme templates, then you'll need to hook into the 'page_template' filter and use a template file you create in your plugin. See: https://wordpress.stackexchange.com/questions/3396/create-custom-page-templates-with-plugins
Depends on the situation, you can add code in your custom template, use shortcodes or custom Gutenberg blocks. You can use Advanced Custom Fields on in the Admin UI and use get_field() in your code, etc. You have lots of options in WordPress. Once you have a custom template you can just add regular PHP in there.
Yes, but you'll need to use regex and create rewrite rules using add_rewrite_rule(). Here are links that help with creating rewrite rules in WordPress
add_rewrite_rule
add_rewrite_tag
flush_rewrite_rules
WP_Rewrite API

How can I make the user change any image on my wp theme just from the theme editor?

How can I make the user change any image on my wp theme just from the theme editor?
For example: I have a background image on my theme footer that i hard-coded, I want to give the user(admin) the ability to change it from the theme editor, and thanks on advanced
I don't want to use something like this:
<div class="footer-background background-image" style="background-image: url(<?php echo get_theme_file_uri( 'assets/images/bg.jpg' ) ?>)" >
If you can just give me a wp codex on this, it would be more than helpful, because I couldn't find any thing related to my problem on Google.
So, you could do something like that (in functions.php) :
add_action( 'customize_register', function( $wp_customize ) {
$wp_customize->add_section(
'section_img',
array(
'title' => 'Images',
'priority' => 35,
)
);
$wp_customize->add_setting(
'footer_image'
);
$wp_customize->add_control(
new WP_Customize_Image_Control(
$wp_customize,
'footer_image',
array(
'label' => 'Footer image',
'section' => 'section_img',
'settings' => 'footer_image'
)
)
);
});
And you get the value by doing (in footer.php) :
get_theme_mod('footer_image');
If I were you, I do followings
Install ACF plugin
Create an option Page
Create new custom field (image field) and assign it to previously created options page.
Update footer template to show the image from back end like this
Update footer image src to get back end ACF field value.
https://www.advancedcustomfields.com/resources/options-page/
https://www.advancedcustomfields.com/resources/image/

Code star framework add metabox to a specific page

I am using the code star framework full version. I have integrated the framework with the theme. How can I show the meta box option only for some specific pages ( like home page and about page )? Currently, the meta box options are showing all pages.
While creating metabox, you should pass "page_templates" value in array. For example, if you want to show metabox on homepage you should do:
$homePage = 'home-page';
CSF::createMetabox($homePage, array(
'title' => 'Homepage',
'post_type' => 'page',
'page_templates' => 'index.php', //filename goes here
'data_type' => 'serialize',
));
For more info: http://codestarframework.com/documentation/#/configurations?id=metabox-option-framework

How to include mailchimp plugin with wordpress theme

I’m working on WordPress theme, and this theme has email subscription widget. So I want to include MailChimp plugin to the theme. How to do that?
Sorry bro you can't use mailchimp plugin in you theme as a embedded plugin. But you can use TGM plugin activation file to include plugin to your theme. Download a copy of TGM class file from here TGM plugin activation class and just copy the class-tgm-plugin-activation.php file to your theme directory and add below codes to your theme's functions.php file ..
<?php
require get_template_directory() . '/class-tgm-plugin-activation.php'; //add your file path where you put the file
add_action( 'tgmpa_register', 'something_register_required_plugins' );
function something_register_required_plugins() {
$plugins = array(
array(
'name' => 'Mailchimp',
'slug' => 'mailchimp',
'required' => true,
),
);
$config = array(
'id' => 'something', // Unique ID for hashing notices for multiple instances of TGMPA.
'default_path' => '', // Default absolute path to bundled plugins.
'menu' => 'tgmpa-install-plugins', // Menu slug.
'parent_slug' => 'themes.php', // Parent menu slug.
'capability' => 'edit_theme_options', // Capability needed to view plugin install page, should be a capability associated with the parent menu used.
'has_notices' => true, // Show admin notices or not.
'dismissable' => true, // If false, a user cannot dismiss the nag message.
'dismiss_msg' => '', // If 'dismissable' is false, this message will be output at top of nag.
'is_automatic' => false, // Automatically activate plugins after installation or not.
'message' => '', // Message to output right before the plugins table.
);
tgmpa( $plugins, $config );
}
?>
This will create a suggestion to your users to install mailchimp plugin from the wordpress.org repository

WP Backend: Create custom menu for specific User Group

I am using Members plugin to manage User Groups (restrict pages/posts for specific user group) now to show the links to that user group in back-end I need a plugin for creating Custom Menu where the allowed pages/posts links will be shown (back-end) for the specific user group.
Sorry, dont know exatly which plugin or code would solve your problem.
But here are some resources:
http://codex.wordpress.org/Roles_and_Capabilities#Resources
http://wordpress.org/extend/plugins/tags/capability
Well what I do is to add this script in my theme's function.php
// Admin bar menu w.r.t role
function wp_admin_bar_new_item() {
global $wp_admin_bar;
if (current_user_can('contractor')){
$wp_admin_bar->add_menu(array(
'id' => 'wp-admin-bar-new-item',
'title' => __('Contractor Menu'),
'href' => 'http://my_site/contractor/'
));
} elseif (current_user_can('consultant')) {
$wp_admin_bar->add_menu(array(
'id' => 'wp-admin-bar-new-item',
'title' => __('Consultant Menu'),
'href' => 'http://my_site/consultant/'
));
}
}
add_action('wp_before_admin_bar_render', 'wp_admin_bar_new_item');
and fix my issue.

Resources