How to use wp_insert_post to create a page with Elementor content? - wordpress-theming

I want to create a page using wp_insert_post but set the page to have Elementor content. Here is the code I have:
// Create post object
$my_post = array(
'post_title' => wp_strip_all_tags( 'Test Page' ),
'post_content' => 'elementor json goes here',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page'
);
// Insert the post into the database
wp_insert_post( $my_post );
But when I do this, the content just prints the actual JSON.

I would create an Elementor template then export it. You then have different options. You can manually import the templates exported into the new website, or set up One Click Demo Import. Popular themes use this plugin to import ready-made templates that used Elementor.
Here is more information about both:
https://powerpackelements.com/how-to-save-import-and-export-templates-in-elementor/
https://wordpress.org/plugins/one-click-demo-import/

Related

Symfony2 - ChoiceType - get choice list from JSON without JS

I have a JSON file with all world languages and would like to put them into choices array inside ChoiceType form field.
$builder->add('languages', ChoiceType::class, array(
'choices' => array()
))
My JSON file is stored: projectname/web/bundles/index/json/languages.json
Is it possible to achieve it without writing JS / AJAX?
P.S. EventListeners or other alternatives that Symfony2 provides suits me well.
You could reach the file with DIR, since I dont' know where the php file with builder is located, it could looks like:
$builder->add('languages', ChoiceType::class, array(
'choices' => json_decode(
//if builder is in controller, this should work
file_get_contents(__DIR__.'/../../../web/bundles/index/json/languages.json'),
true)
));

Wordpress custom page template returns the theme headers

I want to return a JSON array from a Wordpress website. I know this can be done using plugins like WP REST API and JSON API, but I am dealing with custom post types so I want to avoid complexity and write the queries myself.
I have overridden one of my pages by writing a new php file in my theme directory called page-345.php.
Inside my PHP file I have the following code
$args = array(
'post_type' => 'partner',
'post_status' => 'publish',
'posts_per_page' => -1 // all
);
$query = new WP_Query( $args );
$cars = array();
while( $query->have_posts() ) : $query->the_post();
// Add a car entry
$cars[] = array(
'name' => get_the_title()
);
endwhile;
wp_reset_query();
wp_send_json( $cars );
I code works and I get output in JSON. However, JSON in returned alongside the header of the template. (See screenshot)
I also tried entering the code
header( 'Content-type: application/json' );
at the top of my page, but then Wordpress returns an error saying that headers have already been set.

Change name of a post type without losing posts

I have made a site that uses custom posts types for a projects section.
I need to change the post type from 'projects' to 'galleries' but as I have already uploaded a bunch of projects was wondering how I would do this with as little as possible hassle (I do not want to have to re-upload all the images and text etc)
I found a few articles that tell me to do a SQL query to rename the posts
UPDATE `wp_posts`
SET `post_type` = '<new post type name>'
WHERE `post_type` = '<old post type name>';
And this one for the taxonomy
UPDATE `wp_term_taxonomy`
SET `taxonomy` = '<new taxonomy name>'
WHERE `taxonomy` = '<old taxonomy name>';
I just have no idea what I am supposed to do with this code. If it is SQL do I run it in a php file or is there some sort of 'terminal' that can be found in the WP dashboard or cPanel of my site?
Below is how I created my post type (Not sure if this helps)
function create_my_post_types() {
//projects
register_post_type(
'Projects', array('label' => 'Projects','description' => '','public' => true,'show_ui' => true,'show_in_menu' => true, 'menu_position' => 8,'capability_type' => 'post','hierarchical' => false,'rewrite' => array('slug' => '','with_front' => '0'),'query_var' => true,'exclude_from_search' => false,'supports' => array('title','editor','thumbnail'),'taxonomies' => array('category',),'labels' => array (
'name' => 'Projects',
'singular_name' => 'Project',
'menu_name' => 'Projects',
'add_new' => 'Add New Project',
'add_new_item' => 'Add New Project',
'edit' => 'Edit',
'edit_item' => 'Edit Project',
'new_item' => 'New Project',
'view' => 'View Project',
'view_item' => 'View Project',
'search_items' => 'Search Projects',
'not_found' => 'No Projects Found',
'not_found_in_trash' => 'No Projects Found in Trash',
'parent' => 'Parent Projects',
),) );
} // end create_my_post_types
If you have CPanel access, you can look for PHPMyAdmin and run the SQL code there.
Go to PHPMyAdmin.
Select your wordpress database from the left.
RECOMMENDED: Backup your database first, by going to the export tab at the top and doing a quick export.
Select "SQL" from the top tabs.
Copy your SQL queries in the huge textarea, and click Go.
Hope it works!
It's better to go directly with a plugin:
Convert Post Types
This is a utility for converting lots of posts or pages to a custom post type (or vice versa). You can limit the conversion to posts in a single category or children of specific page. You can also assign new taxonomy terms, which will be added to the posts' existing terms.
All the conversion process happens in the function bulk_convert_posts(), using the core functions wp_update_post and wp_set_post_terms. IMO, you should use WordPress functions to do the conversion, there are quite some steps happening in the terms function before the MySQL command.
Do a database backup before proceeding with this kind of operations.

How can I pass output of a widget as content in CJuiTabs in Yii?

How can I pass output of a widget as content in CJuiTabs in Yii?
Here the code I tried and get error:
$this->widget('zii.widgets.jui.CJuiTabs',array(
'tabs'=>array(
'Tab1'=> array('content' => $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$vulnerdataProvider,
'itemView'=>'_latest_vulner' )),
'id' => 'tab1'),
'tab2'=>array('content'=>'Content for tab 2', 'id'=>'tab2'),
),
// additional javascript options for the tabs plugin
'options'=>array(
'collapsible'=>true,
),
));
it gives this error:
Object of class CListView could not be converted to string
Edited: As well as Stu's answer I found this : http://yiibook.blogspot.nl/2012/09/handle-cjuitabs-in-yii.html
Yeah, content expects a string and the widget doesn't return a string. I found this blog piece here: http://mrhandscode.blogspot.com/2012/03/insert-widget-to-another-widget-in-yii.html
The owner found a pretty innovative way round this issue, using output buffering to collect the output of the one widget and then inserting that into the second.
You might be able to achieve it with something like this:
ob_start();
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$vulnerdataProvider,
'itemView'=>'_latest_vulner'
));
$tab1Content=ob_get_contents();
ob_end_clean();
$this->widget('zii.widgets.jui.CJuiTabs',array(
'tabs'=>array(
'Tab1'=> array('content' => $tab1Content,'id' => 'tab1'),
'tab2'=>array('content'=>'Content for tab 2', 'id'=>'tab2'),
),
// additional javascript options for the tabs plugin
'options'=>array(
'collapsible'=>true,
),
));
I've not tested, and may need tinkering!
You can set the second parameter of $this->widget() to true, so the method will return the content of the widget istead of echoing it.
$this->widget('zii.widgets.jui.CJuiTabs',array(
'tabs'=>array(
'Tab1'=> array('content' => $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$vulnerdataProvider,
'itemView'=>'_latest_vulner' ), true),
'id' => 'tab1'),
'tab2'=>array('content'=>'Content for tab 2', 'id'=>'tab2'),
),
// additional javascript options for the tabs plugin
'options'=>array(
'collapsible'=>true,
),
below is ok.
'Tab1'=> array('content' => $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$vulnerdataProvider,
'itemView'=>'_latest_vulner' ), true)

An easy way to load ACL in Zend Framework 2?

I have been following this guide to load my menu configuration and i think it is very nice and clean way to load the menu.
My question is simple, is there a way to load your ACL configuration on the same way with a config array and some kinda of factory?
If there isn't, how do i load a ACL configuration and use with that menu in a easy way?
Thanks!
Edit:
This is a very good blog post on why use modules that is already done and not make your own, http://hounddog.github.com/blog/there-is-a-module-for-that/
ZF2 contains ACL and also RBAC (role based ACL - might be in ZF2.1), but to put it in place, easier is to use module which you can plug into your application. BjyAuthorize seems to me a bit bloated, you have to use ZfcUser module. I prefer ZfcRbac, the ACL rules are based on user roles (group) and their access to controller, action or route. Configuration stored in one config file, really easy to implement.
Most likely there are several ways to do it, but I prefer to do it in getViewHelperConfig() of application's Module.php (here I use BjyAuthorize module to simplify work with ACL, and in particular it allows to set ACL rules in configuration file module.bjyauthorize.global.php)
public function getViewHelperConfig()
{
return array(
'factories' => array(
'navigation' => function($sm) {
$auth = $sm->getServiceLocator()->get('BjyAuthorize\Service\Authorize');
$role = $auth->getIdentityProvider()->getIdentityRoles();
if (is_array($role))
$role = $role[0];
$navigation = $sm->get('Zend\View\Helper\Navigation');
$navigation->setAcl($auth->getAcl())->setRole($role);
return $navigation;
}
)
);
}
Play with This structure . get role and resource from database and save this in session for or any caching .
You are right, there is no out-of-the-box-all-in-one solution. You have to build some bridges between the modules.
Another easy way to integrate BjyAuthorize is using **Zend Navigation**s default methods as described by Rob Allen:
Integrating BjyAuthorize with ZendNavigation
$sm = $e->getApplication()->getServiceManager();
// Add ACL information to the Navigation view helper
$authorize = $sm->get('BjyAuthorizeServiceAuthorize');
$acl = $authorize->getAcl();
$role = $authorize->getIdentity();
ZendViewHelperNavigation::setDefaultAcl($acl);
ZendViewHelperNavigation::setDefaultRole($role);
You can also use ZfcRbac and use a listener to make it work with Zend Navigation.
Since this is a lot of code I simply post the link here:
Check Zend Navigation page permissions with ZfcRbac – Webdevilopers Blog
I've just created an ACL module that creates an ACL Service parsing the routes.
To manage your access control to your application you only need to define roles and add a new key 'roles' in every route. If you do not define that key or its array is empty, then the route becomes public. It also works with child routes.
As an example:
array(
'router' => array(
'routes' => array(
'user\users\view' => array(
'type' => 'Segment',
'options' => array(
'route' => '/admin/users/view/id/:id/',
'constraints' => array(
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'User\Controller\Users',
'action' => 'view',
'roles' => ['admin', 'user'],
),
),
),
),
),
);
The module can be installed via composer and it is now listed in the zend modules repository: http://zfmodules.com/itrascastro/TrascastroACL
You can get more detailed info about use and installation from my blog: http://www.ismaeltrascastro.com/acl-module-zend-framework/