strpos always returns FALSE with wordpress post content - strpos

I am trying to search a string from wordpress post content and see if my substring is present or not. Following is the code
$closing_p = '</p>';
$paragraphs = explode($closing_p, $content);
$search_string="fdfd";
foreach($paragraphs as $index => $paragraph)
{
if (strlen($search_string) > 0)
{
$posi = strpos($paragraphs[$index],$search_string);
if ($posi===FALSE)
{
$insert_ad = false;
}
else
{
$insert_ad = true;
echo "yessssss";
}
}
Content of wordpress post is
"
Welcome to WordPress. This is your first post. Edit or delete it, then start writing!fdfdf"
But it always says not found . Any problems here

Related

ACF overrides HTML attributes added with load_value hook

I'm using ACF together with the ACF Hook acf/load_value to add a custom HTML wrapper to the ACF value. I use then the ACFs to build an Elementor template (I'm using Elementor PRO).
The Template works and the values of ACFs are rendered, but the attribute I've added in the wrapper disappear
I've tried to change the priority of my filters, but it wasn't the problem. I 've also tried to look into the ACF settings, but seems that I cannot change this behavior just changing some settings.
This is the filter I made
if (!function_exists('my_acf_span_property')) {
function my_acf_span_property($value, $property) {
$value = '<span property="' . $property . '">' . $value . '</span>';
return $value;
}
}
if (!function_exists('my_acf_industry_value')) {
function my_acf_industry_value($value)
{
return my_acf_span_property($value, 'industry');
}
}
add_filter('acf/format_value/name=industry', 'my_acf_industry_value');
I made one filter for each ACF I need to change, this is only one as example.
I've tried to debug the filter changing return $value; to return htmlentities($value); in the function my_acf_span_property and the attributes are rendered in the frontend.
The output was expected to be <span property="industry">ACF value</span>
But wat is rendered is <span>ACF value</span>
It could be an Elementor problem?
Any Idea?
I solved with an action to allow the attributes in the posts
if (!function_exists('allow_property_attribute')) {
function allow_property_attribute() {
global $allowedposttags, $allowedtags;
$newattribute = "property";
$allowedposttags["span"][$newattribute] = true;
$allowedtags["span"][$newattribute] = true;
$allowedposttags["div"][$newattribute] = true;
$allowedtags["div"][$newattribute] = true;
$allowedposttags["p"][$newattribute] = true;
$allowedtags["p"][$newattribute] = true;
}
}
add_action( 'init', 'allow_property_attribute' );
It looks like was WordPress my problem, and not Elementor or ACF.

Atom - Emmet package: closing comments

Emmet for Atom: Auto comments after html closing tag.
I can't seem to find a solution to this problem anywhere so I've resorted to asking on here.
http://iaintnoextra.tumblr.com/post/68089741466/automatically-add-closing-comments-to-html-using
In Sublime Text 3, using the emmet user preferences file from the link above, emmet automatically adds comments after a closing html tag; for example:
div.container
would produce:
<div class="container"></div><!-- /.container -->
I can't seem to find anywhere within Emmet's package settings to make this happen on Atom V1. Does anyone know where I can change this so it mimics the same functionality?
http://www.devstavern.com/emmet/custom-comments-in-emmet-sublime-atom-and-others/
With the help of the link above I managed to figure this out on my own, here's the answer:
Edit your emmet settings on Atom.
Update your extensions path under the "Settings" heading to a location where you'd like to save your preferences.json file that we'll create next, I've created a folder in my dropbox folder so I can access the file from any location.
Create a preferences.json file in the folder we just created, add this code below
Comment on same line:
{
"filter.commentAfter": "<!-- /<%= attr('id', '#') %><%= attr('class', '.') %> -->"
}
Comment on new line:
{
"filter.commentAfter": "\n<!-- /<%= attr('id', '#') %><%= attr('class', '.') %> -->"
}
Go to your .atom\packages\emmet\node_modules\emmet\lib folder and edit snippets.json.
Find the html snippet and change the filters setting to "html, c", this will automatically add a comment to the end of any auto-completed code which is shown above.
Ta-da, it works!
Update (05/11/15):
Make sure you restart Atom after saving your changes.
i changed html.js file (tag generator) on C:\Users\\AppData\Roaming\Brackets\extensions\user\brackets-emmet\node_modules\emmet\lib\filter
https://gist.github.com/mgundogdu38/a53af0bccd61bba4cefac56ab705d2b1
Now:
1- i find html tag generator library. html.js.
2- i find html tag generator functions. it is called processTag.
3- i need attribute generator function. it is called makeAttributesString. after i cloned. i called "makeAttributesString2" :)
function makeAttributesString2(node, profile) {
var attrQuote = profile.attributeQuote();
var cursor = profile.cursor();
return node.attributeList().map(function(a) {
var isBoolean = profile.isBoolean(a.name, a.value);
var attrName = profile.attributeName(a.name);
var attrValue = isBoolean ? attrName : a.value;
//i added there. if attribute is id. i added "." on head
if(attrName == "id")
{
return "#"+(attrValue || cursor);
}
//if attribute is class i added "." on head
if(attrName == "class")
{
return "."+(attrValue || cursor);
}
//else only tagname
if (isBoolean && profile.allowCompactBoolean()) {
return ' ' + attrName;
}
//end of my code
}).join('');
}
then i use makeAttributesString2 for generate comment title on proccessTag.
function processTag(item, profile) {
if (!item.parent) { // looks like it's root element
return item;
}
var attrs = makeAttributesString(item, profile);
var cursor = profile.cursor();
var isUnary = abbrUtils.isUnary(item);
var start = '';
var end = '';
// define opening and closing tags
if (!item.isTextNode()) {
//this is pieace of comment title.
var attrsComment = makeAttributesString2(item,profile);
var tagName = profile.tagName(item.name());
if (isUnary) {
start = '<' + tagName + attrs + profile.selfClosing() + '>';
item.end = '';
} else {
//there is comment tag. i just add "<!-- "+tagName+attrsComment+" -->\n" and "\n <!-- /"+tagName+attrsComment+" -->"
start = "<!-- "+tagName+attrsComment+" -->\n"+ '<' + tagName + attrs + '>';
end = '</' + tagName + '>'+"\n <!-- /"+tagName+attrsComment+" -->";
}
}
var placeholder = '%s';
// We can't just replace placeholder with new value because
// JavaScript will treat double $ character as a single one, assuming
// we're using RegExp literal.
item.start = utils.replaceSubstring(item.start, start, item.start.indexOf(placeholder), placeholder);
item.end = utils.replaceSubstring(item.end, end, item.end.indexOf(placeholder), placeholder);
// should we put caret placeholder after opening tag?
if (
!item.children.length
&& !isUnary
&& !~item.content.indexOf(cursor)
&& !tabStops.extract(item.content).tabstops.length
) {
item.start += cursor;
}
return item;
}
If anyone wants to do this with VS Code in 2018, this is what I found to work.
"emmet.preferences": {
"filter.commentAfter": "<!-- /[#ID][.CLASS] -->"
},
"emmet.syntaxProfiles": {
"html": {
"filters": "html, c"
}
}
Add this to your existing User Settings, and it should just work :)

How to minify Wordpress output html code on single line? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
i need to speed up my Wordpress blog. I searched around the web, but no success.
I want to minify or compress my output html code on one(single) line, like Matt Cutt's blog.
I tried W3TC, WP Minify and many others, but without result.
I need script, plugin, function or something that works.
Thanks in advance.
Here is one solution what I give in WordPress.StackExchange.com
https://wordpress.stackexchange.com/a/227896/82023
Generaly, in index.php you can place one code what will compress your HTML using regex. i made and use this on many places and work fine. Is not complete inline but do it's job.
<?php
/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* #package WordPress
*/
/**
* Tells WordPress to load the WordPress theme and output it.
*
* #var bool
*/
define('WP_USE_THEMES', true);
/** Manualy compress WP by Ivijan-Stefan Stipic **/
function compressorCF($str)
{
// clear HEAD
$str = preg_replace_callback('/(?=<head(.*?)>)(.*?)(?<=<\/head>)/s',
function($matches) {
return preg_replace(array(
/* Fix HTML */
'/\>[^\S ]+/s', // strip whitespaces after tags, except space
'/[^\S ]+\</s', // strip whitespaces before tags, except space
'/\>\s+\</', // strip whitespaces between tags
), array(
/* Fix HTML */
'>', // strip whitespaces after tags, except space
'<', // strip whitespaces before tags, except space
'><', // strip whitespaces between tags
), $matches[2]);
}, $str);
// clear BODY
$str = preg_replace_callback('/(?=<body(.*?)>)(.*?)(?<=<\/body>)/s',
function($matches) {
return preg_replace(array(
'/<!--(.*?)-->/s', // delete HTML comments
'#\/\*(.*?)\*\/#s', // delete JavaScript comments
/* Fix HTML */
'/\>[^\S ]+/s', // strip whitespaces after tags, except space
'/[^\S ]+\</s', // strip whitespaces before tags, except space
'/\>\s+\</', // strip whitespaces between tags
), array(
'', // delete HTML comments
'', // delete JavaScript comments
/* Fix HTML */
'>', // strip whitespaces after tags, except space
'<', // strip whitespaces before tags, except space
'><', // strip whitespaces between tags
), $matches[2]);
}, $str);
return $str;
}
/** Loads the WordPress Environment and Template */
ob_start();
require_once( dirname( __FILE__ ) . '/wp-blog-header.php' );
$content=ob_get_clean();
//echo $content;
echo compressorCF($content);
This is really not the best way to speedup your site. If you do it in the template it make the files unreadable and hard to maintain for less than 1% speedup. If you do it with a plugin that process the output, it will slow down the render.
Make sure :
You use as few plugins as possible, for example it's much faster to copy tracking code (google analytics or such) in footer.php than using a plugin
You have compiled, cleaned, minifyied CSS and JS that is on your server and properly compressed files.
You use CDN for all files that are on CDN like JQuery on https://developers.google.com/speed/libraries/devguide
Put mod_expire on your server and set expire date for media files far in future with .htaccess . This will prevent browsers from checking if files have changed (all the 200 status code you see in network traffic analysis)
Cache content using WP supercache or similar plugin
Install APC cache with enough memory (at least 32M for a single WP installation)
If your plugin loads CSS and JS files properly there is no point to say that they will make your website slow.
1. Another issue is how many database call these plugin make to render a particular task.
2. Whether that plugin check data from a remote server to update something like akismat and jetpack, thus makes these two pluin the most resource hungry.
Proper coded theme can help you to load your website properly. like my own site ( http://www.binarynote.com ) score loads score is 99/100 in getmetrix.com
We have Just developed a code in C++ using that we can compress any code. The main function is given here for your ready reference.
//function to compress text files
void compress(char source[], char dest[])
{
ifstream fin(source);
ofstream fout(dest);
char ch;
int sp=0;
while(fin.get(ch))
{
if(ch==' '||ch=='\t')
sp++;
else
sp=0;
if(ch=='\n' || ch=='\r')
fout<<' ';
else
if(sp>=1)
fout<<' ';
else
fout<<ch;
}
fin.close();
fout.close();
}
Put this code to function.php:
class WP_HTML_Compression
{
// Settings
protected $compress_css = true;
protected $compress_js = true;
protected $info_comment = true;
protected $remove_comments = true;
// Variables
protected $html;
public function __construct($html)
{
if (!empty($html))
{
$this->parseHTML($html);
}
}
public function __toString()
{
return $this->html;
}
protected function bottomComment($raw, $compressed)
{
$raw = strlen($raw);
$compressed = strlen($compressed);
$savings = ($raw-$compressed) / $raw * 100;
$savings = round($savings, 2);
return '<!--HTML compressed, size saved '.$savings.'%. From '.$raw.' bytes, now '.$compressed.' bytes-->';
}
protected function minifyHTML($html)
{
$pattern = '/<(?<script>script).*?<\/script\s*>|<(?<style>style).*?<\/style\s*>|<!(?<comment>--).*?-->|<(?<tag>[\/\w.:-]*)(?:".*?"|\'.*?\'|[^\'">]+)*>|(?<text>((<[^!\/\w.:-])?[^<]*)+)|/si';
preg_match_all($pattern, $html, $matches, PREG_SET_ORDER);
$overriding = false;
$raw_tag = false;
// Variable reused for output
$html = '';
foreach ($matches as $token)
{
$tag = (isset($token['tag'])) ? strtolower($token['tag']) : null;
$content = $token[0];
if (is_null($tag))
{
if ( !empty($token['script']) )
{
$strip = $this->compress_js;
}
else if ( !empty($token['style']) )
{
$strip = $this->compress_css;
}
else if ($content == '<!--wp-html-compression no compression-->')
{
$overriding = !$overriding;
// Don't print the comment
continue;
}
else if ($this->remove_comments)
{
if (!$overriding && $raw_tag != 'textarea')
{
// Remove any HTML comments, except MSIE conditional comments
$content = preg_replace('/<!--(?!\s*(?:\[if [^\]]+]|<!|>))(?:(?!-->).)*-->/s', '', $content);
}
}
}
else
{
if ($tag == 'pre' || $tag == 'textarea')
{
$raw_tag = $tag;
}
else if ($tag == '/pre' || $tag == '/textarea')
{
$raw_tag = false;
}
else
{
if ($raw_tag || $overriding)
{
$strip = false;
}
else
{
$strip = true;
// Remove any empty attributes, except:
// action, alt, content, src
$content = preg_replace('/(\s+)(\w++(?<!\baction|\balt|\bcontent|\bsrc)="")/', '$1', $content);
// Remove any space before the end of self-closing XHTML tags
// JavaScript excluded
$content = str_replace(' />', '/>', $content);
}
}
}
if ($strip)
{
$content = $this->removeWhiteSpace($content);
}
$html .= $content;
}
return $html;
}
public function parseHTML($html)
{
$this->html = $this->minifyHTML($html);
if ($this->info_comment)
{
$this->html .= "\n" . $this->bottomComment($html, $this->html);
}
}
protected function removeWhiteSpace($str)
{
$str = str_replace("\t", ' ', $str);
$str = str_replace("\n", '', $str);
$str = str_replace("\r", '', $str);
while (stristr($str, ' '))
{
$str = str_replace(' ', ' ', $str);
}
return $str;
}
}
function wp_html_compression_finish($html)
{
return new WP_HTML_Compression($html);
}
function wp_html_compression_start()
{
ob_start('wp_html_compression_finish');
}
add_action('get_header', 'wp_html_compression_start');
on https://photogrist.com it's works fine :)

JSON API to show Advanced Custom Fields - WordPress

I am developing a magazine WordPress site that will have a json feed for a Mobile App. I set the backend up using Advanced Custom Fields with a Repeater Field for Multiple Articles and Multiple Pages within each article. http://www.advancedcustomfields.com/add-ons/repeater-field/
I am using the JSON API but this does not include any of my custom fields. Is there currently a plugin that can do this?
#Myke: you helped me tremendously. Here's my humble addition:
add_filter('json_api_encode', 'json_api_encode_acf');
function json_api_encode_acf($response)
{
if (isset($response['posts'])) {
foreach ($response['posts'] as $post) {
json_api_add_acf($post); // Add specs to each post
}
}
else if (isset($response['post'])) {
json_api_add_acf($response['post']); // Add a specs property
}
return $response;
}
function json_api_add_acf(&$post)
{
$post->acf = get_fields($post->id);
}
Update for Wordpress 4.7
With the release of Wordpress 4.7 the REST functionality is no longer provided as a distinct plugin, rather its rolled in (no plugin required).
The previous filters don't appear to work. However the following snippet does (can be in your functions.php):
>= PHP 5.3
add_filter('rest_prepare_post', function($response) {
$response->data['acf'] = get_fields($response->data['id']);
return $response;
});
< PHP 5.3
add_filter('rest_prepare_post', 'append_acf');
function append_acf($response) {
$response->data['acf'] = get_fields($response->data['id']);
return $response;
};
Note the filter is a wild card filter, applied like
apply_filters("rest_prepare_$type", ...
so if you have multiple content types (custom), you will need to do:
add_filter('rest_prepare_multiple_choice', 'append_acf');
add_filter('rest_prepare_vocabularies', 'append_acf');
function append_acf($response) {
$response->data['acf'] = get_fields($response->data['id']);
return $response;
};
Note It appears that rest_prepare_x is called per record. So if you are pinging the index endpoint, it will be called multiple times (so you don't need to check if its posts or post)
Came here by searching with the same question. This isn't totally vetted yet but I think this is getting on the right path. Check it out.
I have one less nested level than you do so this might need altered a bit. But the JSON API plugin has a filter called json_api_encode. I have a repeater called specifications that looks like this.
http://d.pr/i/YMvv
In my functions file I have this.
add_filter('json_api_encode', 'my_encode_specs');
function my_encode_specs($response) {
if (isset($response['posts'])) {
foreach ($response['posts'] as $post) {
my_add_specs($post); // Add specs to each post
}
} else if (isset($response['post'])) {
my_add_specs($response['post']); // Add a specs property
}
return $response;
}
function my_add_specs(&$post) {
$post->specs = get_field('specifications', $post->id);
}
Which appends a custom value to the JSON API output. Notice the get_field function from ACF works perfectly here for bringing back the array of the repeater values.
Hope this helps!
There is now a small plugin which adds the filter for you.
https://github.com/PanManAms/WP-JSON-API-ACF
I'm not sure if you're still interested in a solution, but I was able to modify the json-api plugin models/post.php file to display the repeater data as an array. This is a modification of a modification made by http://wordpress-problem.com/marioario-on-plugin-json-api-fixed-get-all-custom-fields-the-right-way/
replace the set_custom_fields_value() function with the following:
function set_custom_fields_value() {
global $json_api;
if ($json_api->include_value('custom_fields') && $json_api->query->custom_fields) {
// Query string params for this query var
$params = trim($json_api->query->custom_fields);
// Get all custom fields if true|all|* is passed
if ($params === "*" || $params === "true" || $params === "all") {
$wp_custom_fields = get_post_custom($this->id);
$this->custom_fields = new stdClass();
// Loop through our custom fields and place on property
foreach($wp_custom_fields as $key => $val) {
if (get_field($key)) {
$this->custom_fields->$key = get_field($key);
} else if ($val) {
// Some fields are stored as serialized arrays.
// This method does not support multidimensionals...
// but didn't see anything wrong with this approach
$current_custom_field = #unserialize($wp_custom_fields[$key][0]);
if (is_array($current_custom_field)) {
// Loop through the unserialized array
foreach($current_custom_field as $sub_key => $sub_val) {
// Lets append these for correct JSON output
$this->custom_fields->$key->$sub_key = $sub_val;
}
} else {
// Break this value of this custom field out of its array
// and place it on the stack like usual
$this->custom_fields->$key = $wp_custom_fields[$key][0];
}
}
}
} else {
// Well this is the old way but with the unserialized array fix
$params = explode(',', $params);
$wp_custom_fields = get_post_custom($this->id);
$this->custom_fields = new stdClass();
foreach ($params as $key) {
if (isset($wp_custom_fields[$key]) && $wp_custom_fields[$key][0] ) {
$current_custom_field = #unserialize($wp_custom_fields[$key][0]);
if (is_array($current_custom_field)) {
foreach($current_custom_field as $sub_key => $sub_val) {
$this->custom_fields->$key->$sub_key = $sub_val;
}
} else {
$this->custom_fields->$key = $wp_custom_fields[$key][0];
}
}
}
}
} else {
unset($this->custom_fields);
}
}
Current version of ACF prints out a custom_fields object on a call to the JSON API, containing all the fields relative to the Post or Page. I edited #Myke version to add specific custom fields from the ACF Option page to each Post or Page. Unfortunately there is not get_fields() function for the whole Option Page so you'll have to edit it depending on your fields structure.
add_filter('json_api_encode', 'json_api_encode_acf');
function json_api_encode_acf($response) {
if (isset($response['posts'])) {
foreach ($response['posts'] as $post) {
json_api_add_acf($post); // Add specs to each post
}
}
else if (isset($response['post'])) {
json_api_add_acf($response['post']); // Add a specs property
}
else if (isset($response['page'])) {
json_api_add_acf($response['page']); // Add a specs to a page
}
return $response;
}
function json_api_add_acf(&$post) {
$post->custom_fields->NAME_OF_YOUR_CUSTOM_FIELD = get_field( 'NAME_OF_YOUR_CUSTOM_FIELD', 'option' );
}

Missing image in cakephp blog post

I am new to cakePHP and I am tring the blog example of cakePHP 1.3 book .
I correctly upload image in this blog example.The image name in database and image in DOCUMENT_ROOT/....correctly
but now I am wanted to show image in my blog with related post.
I am using this code for image upload...
function add() {
if (!empty($this->data)) {
if(isset($this->data["Image"]["image"]["name"])){
$file = new File($this->data["Image"]["image"]["name"]);
$ext = $file->ext();
if ($ext != 'jpg' && $ext != 'jpeg' && $ext != 'gif' && $ext != 'png') {
$this->Session->setFlash('You may only upload image files.');
}else{
if(move_uploaded_file($this->data["Image"]["image"] ["tmp_name"],$_SERVER["DOCUMENT_ROOT"]."test_om/blog/app/webroot/img/upload_image/"
. $this->data["Image"]["image"]["name"]) == true){
$this->data["Post"]["image"] = $this->data["Image"]["image"]["name"];
}
$this->Post->save($this->data);
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
}
}
}
}
and i am showing image form this code
<?php echo $this->Html->image('/img/upload_image/1.gif'); ?>
and this show same image with all post.
but i am wanted to set specfic image with its related post....
If you are sure you are getting everything correct (in the database and the file where it should be) you should use something like this in the view.
<?php echo $this->Html->image($this->data['Post']['image']); ?>
this is assuming you are passing the data from the controller in the way described in the tutorial to a view view :)
if is an index view you should have a variable posts that have all post info, and in the view you will be in a loop like a foreach ($post as $post). Assuming this your view should have something like this:
<?php echo $this->Html->image($post['Post']['image']); ?>
Suggestion: use debug kit (cakephp plugin) so you can see what variables are passed down and the structure (like a pr($variable))
Hope all this helps you, if not, comment this post so i can try to extend my answer if needed