WordPress Insert Post with WP CLI - wordpress

I want to create a post by using WP CLI's eval-file command on a file with the contents below. However, it fails to create a post, and furthermore doesn't return an error even though the second parameter of wp_insert_post() is set to true. Please can someone explain?
<?php
echo 'This file is being evaluated';
$new_post = Array(
'post-title' => 'New Post',
'post-content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Efficiens dici potest. Videmus igitur ut conquiescere ne infantes quidem possint. Aliter enim nosmet ipsos nosse non possumus. An haec ab eo non dicuntur? Non enim iam stirpis bonum quaeret, sed animalis. Duo Reges: constructio interrete. Cave putes quicquam esse verius.'
);
wp_insert_post($new_post, true);

OK, so it was simple:
Use post_title etc., not post-title (underscore vs hyphen!).

Related

xquery: count number of characters and paragraphs in ancestor (div) node up until a certain point in where child node occurs

Xquery newbie here again. I have the following xml:
<div type="section" n="1">
<p>Lorem ipsum dolor sit amet, <rs type="xyz">consectetur</rs> adipiscing <placeName ref="#PLACE1">elit</placeName>, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.<p>
<p>Duis aute irure <rs type="xyz">dolor</rs> in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt <rs type="place" ref="#PLACE2">mollit anim</rs> id est <rs type="xyz">laborum</rs>.<p>
</div>
I would like to create a unique ID for each "place" (rs type=place and placeName) based on its position within the text. To do this, I'd like to retrieve the following information for each place:
paragraph number within the "div type=section" node
character count from the beginning of the paragraph to the start of the child node (rs type=place or placeName).
Taking the above example, I'd expect these results:
<placeName ref="#PLACE1">elit</placeName>
paragraph: 1
character count: 51 ("Lorem ipsum dolor sit amet, consectetur adipiscing ")
<rs type="place" ref="#PLACE2">mollit anim</rs>
paragraph: 2
character count: 186 ("Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt ")
I must be missing something very simple, but I just can't figure out how to do this particular character count in xquery. I know preceding/following-sibling::text() will allow me to count until the previous/following node. Is there something like this to get to the beginning of the ancestor from a given node? Any help or direction would really be appreciated.
If you know the place and rs elements will be children of the p element then you just can select the preceding-sibling::node(), string-join them and then compute the string-length; so in XQuery 3.1 with the arrow operator (I hope exist supports that):
(//placeName | //rs[#type = 'place'])
!
(ancestor::p[1]/(., preceding-sibling::p) => count() ||
' : '
||
preceding-sibling::node() => string-join() => string-length()
)
https://xqueryfiddle.liberty-development.net/bFDb2BK/1
For the more complex case that your elements are descendants of the p I think the expression
(preceding::text() intersect ancestor::p[1]//text()) => string-join() => string-length()
gives the value you want (https://xqueryfiddle.liberty-development.net/bFDb2BK/4), I am not sure how well that performs.
If the arrow and the map operator are not supported or you prefer a FLOWR expression then
for $place in (//placeName | //rs[#type = 'place'])
return ($place/ancestor::p[1]/count((., preceding-sibling::p)) || ' : ' || string-length(string-join($place/preceding-sibling::node())))
for the simple child element case or
for $place in (//placeName | //rs[#type = 'place'])
return (
$place/ancestor::p[1]/count((., preceding-sibling::p))
|| ' : ' || string-length(string-join($place/preceding-sibling::node()))
|| ' : ' || string-length(string-join($place/(preceding::text() intersect ancestor::p[1]//text())))
)
as the descendant case (well, a comparison of both approaches, the last subexpression should work for the descendant case). An alternative to intersect could the use of the << operator: string-length(string-join($place/ancestor::p[1]//text()[. << $place])).

WordPress adds paragraphs around shortcode

I'm trying to create an enclosing shortcode that should be inline with regular text. So I would like to use it like this:
Vestibulum ac diam sit amet quam vehicula [caption]elementum sed sit amet dui[/caption]. Proin eget tortor risus. Vivamus suscipit tortor eget felis porttitor volutpat.
When doing this and saving the post, WordPress will automatically wrap paragraph tags around the shortcode. Like this:
<p>Vestibulum ac diam sit amet quam vehicula</p><p>[caption]elementum sed sit amet dui[/caption]</p><p>. Proin eget tortor risus. Vivamus suscipit tortor eget felis porttitor volutpat. </p>
What's the best way to disable this behaviour? Multiple people suggested moving the wp_autop filter to after the execution of the shortcode, but I believe this will only work for content in the shortcode, not for the wrapping of the shortcode itself.
Also, there's the shortcode_unautop function that should stop WordPress from wrapping the shortcodes, but I'm not sure if it works right, at least I can't seem to get it to work.
My shortcode now looks like this:
function caption_shortcode( $atts, $content = null ) {
return '<span class="caption">' . $content . '</span>';
}
add_shortcode( 'caption', 'caption_shortcode' );
You can use shortcode_unautop() function. For details check here.
Try like this:
function caption_shortcode( $atts, $content = null ) {
return '<span class="caption">' . shortcode_unautop($content) . '</span>';
}
add_shortcode( 'caption', 'caption_shortcode' );

Lodash Templates/Variables not processed in YAML Front-matter for .hbs files

I have been communicating back and forth with the Assemble team on this issue which they think seems to be an implementation issue rather than a bug.
Basically I have an assemble setup (not grunt-assemble) and am trying to pull in some data from a YAML file into a handlebars template via assembles YAML front-matter, passing it to a partial to render out.
What I'm getting when logging assembles data context is that the front matter variables are undefined.
Rather than go through everything I've done and code snippets here you can see this thread for a history of the conversation, code snippets and all things tried thus far: https://github.com/assemble/assemble/issues/758
I have also created a public demo repo on my github for anyone that wants to pull it down / fork for further investigation.
https://github.com/tgdev/assemble-yaml-demo
Why does the lodash template in the front matter return undefined, rather than the contents of the external YAML file?
UPDATE 11/09/16: Using json seems to work fine
Changing the data in the yml files to json (with .json extension) renders as expected with the front-matter middleware in tools/templates.js so it seems as though assemble and/or handlebars is having an issue parsing the yml files.
Here is a copy of the main yml file I'm testing with:
content: >
<h2>Page sub heading</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>
<img src="http://placehold.it/1170x658" alt="" class="align-left" />
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p><img src="http://placehold.it/1170x658" alt="" class="align-right" />
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
So now, my question is why can't the lodash templates in the assemble front-matter of the handlebars template parse the yaml files (undefined in logs)?
Ok so I finally got to the bottom of it.
The problem was this...
Assemble doesn't support external yaml font files so I needed to use this snippet of code just before my middleware.
import yaml from 'js-yaml';
templates.dataLoader('yml', function(str) {
return yaml.safeLoad(str);
});
Then the expander middleware loads the yaml file contents via a lodash template from the front-matter, adding it to the data context.
So here's the whole assemble build task file for reference (I hope it helps someone in the future).
import assemble from 'assemble';
import expander from 'expander';
import merge from 'lodash.merge';
import yaml from 'js-yaml';
import through from 'through2';
import plumber from 'gulp-plumber';
import { projectName, templatesPathConfig as path } from '../project.config';
export default function templates() {
// Create assemble instance
let templates = assemble();
function expand(data) {
// `data` is front-matter
const ctx = merge({}, templates.cache.data, data);
return expander.process(ctx, data);
}
// #reference
// https://github.com/node-base/base-data#dataloader
//
// #notes
// Loading yaml files is not built in. Assemble uses
// base-data now. You can add yaml loading by using
// a custom dataLoader.
templates.dataLoader('yml', function(str) {
return yaml.safeLoad(str);
});
templates.data(path.data); // path.data = ./src/templates/data/**/*.yml
templates.preRender(/\.(hbs|html)$/, function (view, next) {
view.data = expand(view.data);
next();
});
templates.task('preload', function(cb) {
templates.partials(path.partials);
templates.layouts(path.layouts);
// Register helpers
templates.helpers(path.helpers);
// Add master pages and listing page
templates.pages(path.pages);
templates.pages(path.referencePages);
templates.pages(path.index);
// Add custom data
templates.data({
projectName: projectName
});
cb();
});
// Assemble task to build template files
templates.task('build', ['preload'], () => {
// Render out the template files to 'dist'
return templates.toStream('pages')
.pipe(plumber({
errorHandler: err => {
log.error(`${err.message}.`);
}
}))
.pipe(templates.renderFile())
.pipe(plumber.stop())
.pipe(renameExt())
.pipe(templates.dest('dist'));
});
// Run the Assemble build method
templates.build('build');
}
// Change the file extension through node stream
function renameExt() {
return through.obj( (file, enc, next) => {
file.extname = '.html';
next(null, file);
});
}

wordpress plugin different content on different pages

Can you please help me with following? I am building plugin that will based on different pages show different content.
For example, on page1 there will be one content, on page2 other, and on all other the content will stay as it was.
Here is my plugin code (I take content and change it for specific pages only):
$content = get_the_content();
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
wp_enqueue_script('ajax_bill', '/wp-content/plugins/ed-merna-mesta/js/ajax_bill.js');
wp_enqueue_style('styles', '/wp-content/plugins/ed-merna-mesta/css/styles.css');
include_once("php/functions.php");
include_once("php/functions_helper.php");
add_filter('the_content','get_plugin_page_content', $content);
function get_plugin_page_content($content) {
if (is_page('page1')) {
get_accounts_list();
}
else if (is_page('page2'))
{
last_account_bill();
}
else
echo $content;
}
Problem is that home page show something like (I have some random text):
[one_third]
A lot of fonts included
[icon_pencil]Quisque convallis orci ut lacus sed imperdiet eget lobortis dolor tellus pharetra. In hac dolor habitasse platea dictumst. Integer sedelor risus sit mi ligula. Lorem ipsum dolor sit amet platea in distumst. Quisque convallis orci ut lacus sed imperdiet eget lobortis dolor tellus pharetra. [button url="#"]Read more...[/button] [/one_third] [one_third]
There is some problem with showing the content!
When I comment line:
add_filter('the_content','get_plugin_page_content', $content);
everything is OK on home page (plugin not showing anything of course).
I am using clear-theme.
Where am I going wrong???
Thank you.
if anyone stumbles to same problem, I figured it out:
I needed:
do_shortcode( $content );

Drupal JQuery UI Draggable / Stacking Divs

I'm trying to get the jquery draggable function to work, particularly the stacking feature, in Drupal. I've got the JQuery UI module installed and it works fine with a hide/reveal sliding box but not for the draggable?
Here's what I've got in the body of my page:
<?php
drupal_add_js('
$(document).ready(function drag() {
$(".cc-drag").draggable();
});','inline');
?>
<div class="cc-drag">
<div class="cc_top">
<div class="cc_content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque elit dolor, ornare non vulputate quis, dictum ut neque.
Nunc non velit at nulla posuere pulvinar. Maecenas vitae diam iaculis lorem sagittis condimentum et at elit.
Praesent ac augue dolor.
Sed sit amet orci leo, vitae sagittis ante.
Phasellus id volutpat nibh.
Nam ullamcorper mi at urna cursus vitae aliquet est ullamcorper.
</div>
</div>
<div class="cc_bottom"></div>
</div>
I'm using Drupal 6.x on WAMP.
Thanks!
In Drupal 7, where jQuery UI is in core:
drupal_add_library('system', 'ui.draggable');
See http://drupal.org/node/1001508
In Drupal 6:
jquery_ui_add(array('ui.draggable'));
This is the preferred method:
jquery_ui_add(array('ui.draggable', 'ui.resizable', 'ui.dialog'));
Just place that in your module's hook_init() function.
For some reason the js files weren't loading, or not loading fully, so I copied the ones I needed to my theme folder and linked to them in my .info file.
scripts[] = js/jquery.ui.widget.js
scripts[] = js/jquery.ui.mouse.js
scripts[] = js/jquery.ui.core.js
scripts[] = js/jquery.ui.draggable.js
The order they are loaded is important. I found many many other threads and forums where other people have been having the same problem in the last 1-4 months. Hope this helps someone else. The js console in firebug was really helpful in getting to the root of the errors, if anyone needs to explore their own problem further.

Resources