Hey everyone Im trying to retrieve mathml from a mysql database through a custom plugin I have created for CKeditor. But for some reason whenever paragraph tags are inserted with the mathml, the plugin I am trying to create refuses to open. I suspect this has something to do with using htmlentities. I would appreciate it if someone could provide some insight on how to apply htmlentities when retrieving the data in a selectbox of the plugin dialog. I actually want to know how to use the htmlentities function within items:
items:[
<?php $i = 1;
while($row = mysql_fetch_assoc($queryResource))
{
if($i == 1) {
echo "['$row[expressionname]','$row[mathexpression]']";
$i = null;
}
else
echo "['$row[expressionname]','$row[mathexpression]']";
}
?>],
commit : function( data )
{
data.equations = this.getValue();
}
}]
Related
I'm using gutenberg gallery block inside a post and I'm trying to create a button which contains all of the image ids in the gallery block as html data attributes such that later when I output the content to the page I can have access to those ids using javascript. Basically I'm trying to create a lightbox feature for a custom post type.
The problem is that I can't get access to the gutenberg gallery block data.
Here's my code
while ($custom_post_type->have_posts()) {
$custom_post_type->the_post();
$gallery = get_post_gallery(get_the_id(), false);
$ids = explode(",", $gallery['ids']);
}
And here's that button with html data attributes
<button class="gallery"
<?php
for ($i = 0; $i < count($ids); $i++) {
$img_link = wp_get_attachment_image_url($ids[$i], 'full');
echo "data-img-" . $i . " = " . $img_link . " ";
}?>
>
Light-box
</button>
But it does not work, $ids is empty. It prints out this
<button class="gallery">Light-box</button>
Thanks for your help!
Edit
I'm using wordpress blocks on the post page, I'm not quite certain how they have been generated, but they work out of the box.
"it does not work, $ids is empty."
That block is one of the default wordpress blocks, aka "core blocks". In order to have access to its data you would need to use parse_blocks function not get_post_gallery. That's why your variable is empty.
So the overall workflow to get what you're looking for would be:
Check whether your post has any blocks or not, using has_block function. has_blockDocs
If it does, then get all of the blocks (including gallery block) using parse_blocks function. parse_blocksDocs
parse_blocks will return an array of all blocks used in your post, so loop through them and see which one is called "core/gallery".
"core/gallery" block has "attributes" and "ids" for each image you've added in the admin panel.
Once you get the "ids", you should be able to create your custom button and image links using wp_get_attachment_image_url function. wp_get_attachment_image_urlDocs
As a POC:
Please see the following code:
if (has_block("gallery", get_the_content()))
{
$post_blocks = parse_blocks(get_the_content());
foreach ($post_blocks as $post_block)
{
if ("core/gallery" == $post_block["blockName"])
{
$ids = $post_block["attrs"]["ids"];
}
}
}
And this is the button:
<button class="gallery"
<?php
for ($i = 0; $i < count($ids); $i++) {
$img_link = wp_get_attachment_image_url($ids[$i], "full");
echo "data-img-" . $i . " = " . $img_link . " ";
}
?>
>
Light Box
</button>
Which will return:
Note:
I've used get_the_content function, assuming that you're in the loop based on the code you provided in your question. If you're not in the loop or you would need to use the code outside of the loop you could use global $post; $post->post_content; instead.
This answer has been tested on wordpress 5.8 and works.
I am struggling with custom field to insert page specific CSS or JS in Wordpress.
I was able to insert single CSS following the article.
http://www.wpbeginner.com/wp-themes/embed-custom-css-in-your-single-posts-with-custom-fields/
Then I was wondering what if I want to insert multiple CSSs via a custom field.
I guess I have to do with Arrays and loops, right?
Can I just want put multiple CSSs in one field by separating with commas or something?
Example:
in the custom field section:
field name: customCSS
filed value: foo.css, bar.css, other.css ...
Could someone give me an idea how to do it?
Thank you.
My wordpress ver. is 3.8.
I found a solution to this. basic PHP stuff...
I dont know if this is efficient, but I got what I want as a result.
I used comma(,) as a delimiter in the custom field.
if (is_page()) {
$uniqueCSS = get_post_meta($post->ID, uniqueCSS ,true);
$uniqueCSSArr = explode(',', $uniqueCSS);
if($uniqueCSSArr) {
for( $i = 0; $i < count($uniqueCSSArr); $i++ ) {
echo '<link rel="stylesheet" href="'. get_template_directory_uri() .'/css/'. $uniqueCSSArr[$i] .'"/>';
echo "\n";
}
}
}
Thank you
I was trying to generate a Javascript varialbe using php. I am getting the desired result on the source page but it looks like that result is not being processed into the array. Is there any way of doing it using javascript? Here, I'm generating URLs for images that need to be displayed on my website carousel and though a for loop would save me the time of entering every url. The images are also number serially. Since I'm not well versed in javascript can you suggest me a javascript alternative?
var leftrightslide=new Array()
var finalslide=''
<?php for($i=0;$i<34;$i++) {
$j=$i+1;
echo "leftrightslide[".$i."]='<a href='#'><img src='../images/".$j.".jpg' border=0></a>'\n";
}
?>
You can do it using javascript only. No reason for using PHP here.
var leftrightslide = new Array()
var finalslide = ''; // this line is not really relevant to the question
for (var i = 0; i < 34; i++){
var j = i + 1;
leftrightslide[i] = '<img src="../images/'+ j +'.jpg" border="0">';
}
echo "leftrightslide[".$i."]='<img src=\"../images/".$j.".jpg\" border=0>';";
Here's a snippet of code that I use to move data from PHP To JS
if (isset($javascriptData)) {
echo "<script>";
foreach(array_keys($javascriptData) as $jsData) {
echo "var " . $jsData . " = " . json_encode($javascriptData[$jsData]) . ";\n";
}
echo "</script>";
}
I pass in $javascriptData to my view which is an array with the structure array('JS_VAR_NAME' => 'JS_VALUE')
You can then use those variables in any scripts you've added below that
Since your example code contains no script tags, or other HTML elements for that matter, one might assume that this PHP snippet is intended to generate some JavaScript source "file" external to the page in which it is being used.
If that is the case, consider that the following additional line may just fix it:
<?php header( 'Content-Type: text/javascript' ); ?>
var leftrightslide=new Array()
var finalslide=''
<?php for($i=0;$i<34;$i++) {
$j=$i+1;
echo "leftrightslide[".$i."]='<a href='#'><img src='../images/".$j.".jpg' border=0></a>'\n";
}
?>
I'm using google Line chart for my college project, here i want to add rows dynamically based on the user selection from database, addRow() function is used to add a row but i want it to be add by looping. someone can help me?
Here is my code:
var rowArray1 = [];
var rowArray2 = [];
<?php
for($i=1;$i<=$count;$i++)
{
$row=mysql_fetch_array($rows);
echo "rowArray1.push('". $row['a'] ."')";
echo "rowArray2.push(". $row['b'].")";
array_push($rowArray,"'".$row['a']."',".$row['b']);
}
?>
for(i=0;i<count;i++)
{
data.addRow( [rowArray1[i], rowArray2[i]] );
}
it's not working properly... :-(
Finally I got the solution. it's very simple way. when i asked this question, i was just a beginner so i dont know how to do it. now i got the answer.
I just included the PHP scripts inside the javascript code like this,
<script>
// Google chart codes....
<?php
$row=mysql_fetch_array($rows);
foreach($row as $data) {
echo "data.addRow( $data['a'] , $data['b'] );";
}
?>
// Google chart codes....
</script>
It's worked fine... :)
I'm currently trying to create a site for TV shows and due to certain wordpress limitations this is becoming a challenge.
However I bypass that with the use of implementing custom meta fields in the functions.php file, now my problem is that I need it to actively create new fields when I submit information in the current field. For example custom metabox names are
(Episode Name="This is It") (Episode Number="1") (Season Number="5")
Instead of having to create all the boxes from the beginning I would like the use Javascript (jQuery) or any solution to automatically create a new set of these 3 boxes
(Episode Name="") (Episode Number="") (Season Number="")
so I can just enter the new information as they come. Thank you in advance for your help!
Note: I have invested too much time into wordpress to just switch to another cms, so that is not an option at this point in time.
from what I understand of your question, you are looking for a simple solution to automate the input process. I have a general idea of what it is you nee to achieve as I have had to do something similar on a brochure type of website.
I have tried answering your question using Jquery, but I find it to increase the amount of text input required when creating your post, there is unfortunately no completely automated method of doing it, but hopefully below would provide you with a solution.
first I found the following plugin: Types - Complete Solution for Custom Fields and Types here: http://wordpress.org/extend/plugins/types/
This allows you to create custom meta feilds when creating a new post/page. The custom feilds are brought added with a perfix of "wpcf-" and then the name of the field, e.g. "Episode Name" becomes "wpcf-episode-name" in the database.
The following is an edit of wordpress's get_meta function:
function get_specifications(){
if ( $keys = get_post_custom_keys() ) {
echo '<div class="entry_specifications">';
foreach ( (array) $keys as $key ) {
$keyt = trim($key);
if ( '_' == $keyt[0] )
continue;
$values = array_map('trim', get_post_custom_values($key));
$value = implode($values,', ');
//remove "wpcf-"
$key = str_replace('wpcf-','',$key);
//convert "-" to a space " "
$key = str_replace('-',' ',$key);
//check if it has a value, continue if it does, skip if it doesn't:
if ($value <> ''){
echo apply_filters('the_meta_key', "
<div class='meta_wrapper'>
<div class='meta_title'>$key</div>
<div class='meta_value'>$value</div>
</div>\n", $key, $value);
};
}
}
// echo '</div>'; comment out and remove the line below if you are not using floats in your css
echo '</div><div class="clear"></div>';
}
In my page.php (or your template-page.php) I have added the following code when/where I want the meta to be produced:
<?php
//check to see if there is meta data, as you only want the meta data on TV Program pages
$met_check = get_post_meta($post->ID, 'wpcf-features', true);
if ($met_check <> ''){
//if it has a value, spit out "About EpisodeName" ?>
<h2 class="post-title clear">About <?php echo $title ?></h2>
<?php //perform the function from functions.php and return results:
echo get_specifications();
}
?>
I've styled the result with the following CSS:
.meta_entry {
clear:both;
}
.meta_title {
float:left;
text-transform:capitalize;
width:200px;
}
.meta_value {
float:left;
width:480px;
}
Hope this helps mate!
There is a wonderful plugin for wordpress called Pods which might be a viable solution.
Try http://wordpress.org/extend/plugins/custom-field-template/