Wordpress CodeMirror.fromTextArea is not a function error - wordpress

I am using code mirror editor from wordpress bundle in a plugin page. I have enqueued the following styles and scripts as per some research here.
wp_enqueue_style('wp-codemirror');
$cm_settings['codeEditor'] = wp_enqueue_code_editor(array('type' => 'text/css'));
$var = array(
'cm_settings' => $cm_settings
);
wp_localize_script('my-admin-script', 'my_var', $var);
Then in my js file, i have added the following line to make the textarea my_textarea to code editor.
wp.codeEditor.initialize($('#my_textarea'), my_var.cm_settings);
Everything works fine. I am not able to get value from the code mirror editor. I tried
var editor = CodeMirror.fromTextArea(document.getElementById("my_textarea"));
console.log(editor.getValue());
But i am getting an error saying CodeMirror.fromTextArea is not a function. I have been stuck at this the whole day. Any helps are appreciated.

The issue was solved on setting a variable,
var editor = wp.codeEditor.initialize($('#my_textarea'), my_var.cm_settings);
and accessing the varibale editor, you can do anything.
for eg: you can use editor.codemirror.getValue() to access the value. You don't need to use CodeMirror.fromTextArea here.

Related

Using a Composer class in functions.php

I'm trying to add a shortcode to my Wordpress site to pull a piece of data from a Google spreadsheet and drop it into a page. To do this, I'm trying to use Sheetsu. The php libraries for Sheetsu are managed through Composer.
I've got a working piece of standalone code, but when I drop it in functions.php, like this...
function do_sheetsu() {
require('vendor/autoload.php');
use Sheetsu\Sheetsu;
$sheetsu = new Sheetsu(['sheetId' => '8b297aa81110']);
$response = $sheetsu->search(['id' => '2.05.1']);
$collection = $response->getCollection();
echo $collection->get(0)->answer;
}
add_shortcode('sheetsu','do_sheetsu');
...it blanks my site. If I comment out the use Sheetsu\Sheetsu; line, my site comes back, but I get no output, and the error "PHP Fatal error: Class 'Sheetsu' not found" which I suppose makes perfectly good sense.
I know enough php to be able to break things, apparently, and my knowledge of Composer comes mostly from messing around with Flarum a little bit.
I'm sure I'm missing something obvious here, I'm guessing involving namespace declarations or something, but I can't put the pieces together.
I'm looking suspiciously at my composer.json file, as well—something doesn't seem right, but I'm not sure what to fix.
For the record, my composer.json, composer.lock, and vendor folder are in my theme folder with functions.php. My composer.json file looks like this:
{
"require": {
"emilianozublena/sheetsu-php": "^0.0.6"
}
}
and I'm not sure it should.
But what's more troubling is finding a way around that use Sheetsu\Sheetsu line that seems to totally break Wordpress...
Thanks for any help!
I assume here you have installed the package using command line running composer install or composer require emilianozublena/sheetsu-php.
You cannot use the use php keyword inside function. The use keyword must be declared in the outermost scope of a file (the global scope). Refer to this answer for more detail here
So, in this condition you can chain the namespace while instantiating your class. In our case new Sheetsu(['sheetId' => '8b297aa81110']) becomes new \Sheetsu\Sheetsu(['sheetId' => '8b297aa81110']);
Try this code below
function do_sheetsu() {
require('vendor/autoload.php');
$sheetsu = new \Sheetsu\Sheetsu(['sheetId' => '8b297aa81110']);
$response = $sheetsu->search(['id' => '2.05.1']);
$collection = $response->getCollection();
echo $collection->get(0)->answer;
}
add_shortcode('sheetsu','do_sheetsu');

WordPress Unyson - Share page options

I'm using the unyson Framework for WordPress and currently using page options which work just fine on that particular page.
But I want to be able to access those options when on another page.
Here is my php -
$page = fw()->theme->get_options( 'service-settings' );
<?php echo wp_kses_post($page['service']['options']['service-box']['options']['description']; ?>
But this doesn't allow me the content from the array.
Using the following I can see the array, but cannot get the data.
fw_print($page);
Thanks guys
You're receiving just options array by calling fw()->theme->get_options('slug') - literally what you typed in the framework-customizations/theme/options/slug.php file.
If you want to receive the actual data you need to use DB helpers.
$data = fw_get_db_settings_option();
// or even refer to individual options, for performance's sake
$individual_option = fw_get_db_settings_option('option_id');
fw_print($data);
fw_print($individual_option);

Wordpress and Woocmmerce - how to access settings from theme?

Im trying to pull the settings from Woocommerce admin. I found this:
http://oik-plugins.eu/woocommerce-a2z/oik_api/woocommerce_settings_get_option/
$string = woocommerce_settings_get_option( $option_name, $default );
It looks to be a public function but I cannot access from my theme files. It just gives me Fatal error: Call to undefined. Anyone have any idea how you can access the setting from the theme?
I'm trying to get 'woocommerce_frontend_css_primary', $colors['primary'] so can tie them into the rest of the theme. Woocommerce currently just write the values directly to .less file.
Woocommerce docs are a bit misleading, but it turns out there is another function called get_option... as long as you know the name of the option you can use. EG. get array of front end colors:
$woo_styles = get_option( 'woocommerce_frontend_css_colors' );

Drupal 6 preprocess function for custom template

I have a custom template file I'm using for a page.
Say the page is www.mydomain.com/hello-world
The content of this page is taken from a template file called hello-world.tpl.php
Now I need to have a variable prepared in advance for that page, so I took a look at how core modules achieve this and tried to implement in the same way, only the variables are always null..
for hello-world.tpl.php I created in my module file a function called:
function template_preprocess_hello_world(&$variables) {
$variables['test'] = "test";
}
As I said $test doesn't get any value on www.mydomain.com/hello-world (I receive NULL when var dumping it)
I spent an hour or so double checking that I don't have any typos or anything like that.
Is what I'm doing worng??
Try renaming the function to mymodulename_preprocess_hello_world(&$variables). Don't forget to clear the cache as well.

Grabbing Get option function from php file in plugin folders

Really need some help i am making a plugin within the main plugin i have the code.
// Some Defaults
$bucket = 'music';
// Put our defaults in the "wp-options" table
add_option("isd-bucket", $bucket);
so thats great for me i no it adds to the database music.
the problem i am have is i need to pull that out off the database into another php file in my plugin folder.
in my playlist.php i have.
echo get_option("isd-bucket");
problem is when running this file i get.
Call to undefined function get_option() in
I understand this probably means i need to require other files so it intergrates with wordpress i have tried many options but cant get it to work plese help.
Thanks
Be sure to remove the extra white space (blank line) between the lines of code.
// Some Defaults
$bucket = 'music';
// Put our defaults in the "wp-options" table
add_option( "isd-bucket" , $bucket );

Resources